创建一个java程序:按下鼠标,然后等到鼠标释放以后得到一个矩形;重复操作N次,窗口上就有N个矩形。 10
这是我写的代码,但是好像只能生成一个矩形。大神们,帮我看看该怎么修改程序啊importjava.awt.Color;importjava.awt.Graphics;imp...
这是我写的代码,但是好像只能生成一个矩形。大神们,帮我看看该怎么修改程序啊
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Rectangle extends JLabel {
private int x1 = 0;
private int y1 = 0;
private int x2 = 0;
private int y2 = 0;
private boolean visit = true;
public Rectangle() {
super();
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
x1 = e.getX();
y1 = e.getY();
}
public void mouseReleased(MouseEvent e) {
x2 = e.getX();
y2 = e.getY();
visit = true;
System.out.println("坐标1:"+x1+" "+y1);
System.out.println("坐标2:"+x2+" "+y2);
repaint();
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
x2 = e.getX();
y2 = e.getY();
visit = false;
repaint();
}
});
}
protected void paintComponent(Graphics g) {
if (!visit)
return;
g.setColor(Color.black);
g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2),
Math.abs(y1 - y2));
}
super.paintComponent(g);
}
protected void paintBorder(Graphics g) {
g.setColor(getForeground());
if (visit)
return;
g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2),
Math.abs(y1 - y2));
}
public static void main(String[] args) {
JLabel rectangle = new Rectangle();
JFrame frame = new JFrame();
frame.getContentPane().add(rectangle);
frame.setSize(300, 300);
frame.setVisible(true);
}
} 展开
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Rectangle extends JLabel {
private int x1 = 0;
private int y1 = 0;
private int x2 = 0;
private int y2 = 0;
private boolean visit = true;
public Rectangle() {
super();
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
x1 = e.getX();
y1 = e.getY();
}
public void mouseReleased(MouseEvent e) {
x2 = e.getX();
y2 = e.getY();
visit = true;
System.out.println("坐标1:"+x1+" "+y1);
System.out.println("坐标2:"+x2+" "+y2);
repaint();
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
x2 = e.getX();
y2 = e.getY();
visit = false;
repaint();
}
});
}
protected void paintComponent(Graphics g) {
if (!visit)
return;
g.setColor(Color.black);
g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2),
Math.abs(y1 - y2));
}
super.paintComponent(g);
}
protected void paintBorder(Graphics g) {
g.setColor(getForeground());
if (visit)
return;
g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2),
Math.abs(y1 - y2));
}
public static void main(String[] args) {
JLabel rectangle = new Rectangle();
JFrame frame = new JFrame();
frame.getContentPane().add(rectangle);
frame.setSize(300, 300);
frame.setVisible(true);
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询