用drawRect()画出矩形,如何实现脱拽功能
本来不想问,但是做了一个多小时都搞不出来,找相关的帖又找不到,只好麻烦下大家了!~import java.awt.*;
import java.awt.event.*;
public class DrawRect extends Frame
{
private int x,y,width,higth;
public DrawRect(){
setTitle("画图");
setBackground(Color.WHITE);
setSize(400,400);
setVisible(true);
addMouseListener(new
MouseAdapter(){
public void mousePressed(MouseEvent e){
x=e.getX();
y=e.getY();
}
public void mouseReleased(MouseEvent e){
width=e.getX();
higth=e.getY();
}
});
addWindowListener(new
WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public void paint(Graphics g){
g.drawRect(x,y,width-x,higth-y);//为什么我这里不可以画出矩形哦??
//g.drawRect(50,50,120,230);
}
public static void main(String[] args)
{
new DrawRect();
}
}
为什么我这样写画不了,更不用说实现脱拽功能 了
还要写些什么函数吗!~那位可以帮我补全下!~
谢谢先!~
----------------解决方案--------------------------------------------------------