实现了MouseMotionListner接口后,便可以捕获鼠标事件了。 当鼠标进入到某个特定区域时便会触发MouseMoved事件,你便能捕获到这个移动的动作了。通过一个例子来看下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | import java.awt.event.*; import javax.swing.*; public class MouseCaptureDemo extends JFrame implements MouseMotionListener { public JLabel mouseHoverStatus; public static void main(String args[]) { new MouseCaptureDemo(); } MouseCaptureDemo() { setSize( 500 , 500 ); setTitle( "Frame displaying Coordinates of Mouse Motion" ); mouseHoverStatus = new JLabel( "No Mouse Hover Detected." , JLabel.CENTER); add(mouseHoverStatus); addMouseMotionListener( this ); setVisible( true ); } public void mouseMoved(MouseEvent e) { mouseHoverStatus.setText( "Mouse Cursor Coordinates => X:" +e.getX()+ " | Y:" +e.getY()); } public void mouseDragged(MouseEvent e) {} } FileOutputStream Vs. FileWriter |
已有 22658 名学员学习以下课程通过考试
最需教育客户端 软件问题一手掌握
去 App Store 免费下载 iOS 客户端
点击加载更多评论>>