Java操作應用——捕獲鼠標動作
實現(xiàn)了MouseMotionListner接口后,便可以捕獲鼠標事件了。 當鼠標進入到某個特定區(qū)域時便會觸發(fā)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 |
點擊加載更多評論>>