importjava.awt.*;importjava.awt.event.*;importjavax.swing.SwingUtilities;publicclassW14E6{publicstaticvoidmain(String[]args){MyWindowmyWin=newMyWindow("鼠标运动事件测试")...
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,MouseListener{
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);
ta.addMouseListener(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){}
public void mouseClicked(MouseEvent e){
ta.append("鼠标点击,位置"+"("+e.getX()+","+e.getY()+")\n");
}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
}
展开