求大家帮助修改下
修改例W14E6,使得当鼠标在窗口中点击任何位置时,都会将按钮放在以点击位置为中心的位置,并在文本域中显示中心位置的坐标。import java.awt.*;
import java.awt.event.*;
import javax.swing.SwingUtilities;
public class W14E6{
public static void main(String[] args) {
MyWindow myWin = new MyWindow("鼠标运动事件测试");
}
}
class MyWindow extends Frame implements MouseMotionListener{
Button b;
TextArea ta;
MyWindow(String s){
super(s);
setLayout(new FlowLayout());
setBounds(200, 100, 600, 300);
b = new Button("按钮");
add(b);
ta = new TextArea();
add(ta);
b.addMouseMotionListener(this);
ta.addMouseMotionListener(this);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
setVisible(true);
}
public void mouseDragged(MouseEvent e){
int x,y,w,h;
Component c = (Component)e.getSource();
e = SwingUtilities.convertMouseEvent(c,e,this);
x = e.getX();
y = e.getY();
w=c.getSize().width;
h=c.getSize().height;
c.setLocation(x-w/2,y-h/2);
}
public void mouseMoved(MouseEvent e){}
}