有没有人知道怎么样在JFrame里面实现用鼠标画一个矩形啊??
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DrawRectangle extends JFrame implements MouseListener{
private int mouseX,mouseY,mouseWidth,mouseHeight;
public DrawRectangle(){
super("鼠标画矩形");
this.setSize(640,480);
this.setVisible(true);
this.addMouseListener(this);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new DrawRectangle();
}
public void paint(Graphics g){
super.paint(g);
g.setColor(Color.blue);
//g.clearRect(0,0,this.getSize().width,this.getSize().height);
g.drawRect(mouseX,mouseY,mouseWidth,mouseHeight);
}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){
mouseX = e.getX();
mouseY = e.getY();
}
public void mouseReleased(MouseEvent e){
mouseWidth = e.getX()-mouseX;
mouseHeight = e.getY()-mouseY;
repaint();
}
}