首页 > 代码库 > Java 鼠标坐标显示事件监听MouseMotionListener

Java 鼠标坐标显示事件监听MouseMotionListener

1、定义鼠标的坐标  当鼠标移动时发生显示事件

方法摘要
 voidmouseDragged(MouseEvent e)
          鼠标按键在组件上按下并拖动时调用。
 voidmouseMoved(MouseEvent e)
          鼠标光标移动到组件上但无按键按下时调用。

 

 1 public class TestKeyAndMouse extends Frame implements MouseMotionListener 2 { 3     static TextField field; 4      5     public static void main(String[] args) { 6         TestKeyAndMouse frame = new TestKeyAndMouse(); 7          8         frame.setSize(500, 300); 9         10         //frame.addKeyListener(frame);11         frame.addMouseMotionListener(frame);12         13         field = new TextField();14         frame.add(field,BorderLayout.SOUTH);15         16         frame.setVisible(true);17     }18 19     @Override20     public void mouseDragged(MouseEvent e) {21         22     }23     24     //鼠标移动事件25     @Override26     public void mouseMoved(MouseEvent e) 27     {28         field.setText("您鼠标的当前作标为X:" + e.getX()+ " Y:" + e.getY());29     }30 }

 

技术分享

Java 鼠标坐标显示事件监听MouseMotionListener