Java实现WindowsMouse监听器

Posted 进击的巨人

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java实现WindowsMouse监听器相关的知识,希望对你有一定的参考价值。

1、通过实现WindowListener接口来实现Windows监听器:

 1 import java.awt.event.WindowEvent;
 2 import java.awt.event.WindowListener;
 3 
 4 import javax.swing.JFrame;
 5 
 6 public class WindowsListenerFrame extends JFrame implements WindowListener  {
 7     
 8     public WindowsListenerFrame() {
 9         this.setTitle("窗体事件");
10         setSize(600, 400);
11         setResizable(false);
12         setLocationRelativeTo(null);
13         
14         this.addWindowListener(this);
15     }
16 
17     public static void main(String[] args) {
18         JFrame frame = new WindowsListenerFrame();
19         frame.setVisible(true);
20     }
21 
22     @Override
23     public void windowActivated(WindowEvent event) {
24     }
25 
26     @Override
27     public void windowClosed(WindowEvent arg0) {
28     }
29 
30     @Override
31     public void windowClosing(WindowEvent arg0) {
32         this.dispose();
33     }
34 
35     @Override
36     public void windowDeactivated(WindowEvent arg0) {
37     }
38 
39     @Override
40     public void windowDeiconified(WindowEvent arg0) {
41     }
42 
43     @Override
44     public void windowIconified(WindowEvent arg0) {
45     }
46 
47     @Override
48     public void windowOpened(WindowEvent arg0) {
49     }
50 }

2、通过实现WindowAdapter接口来实现Windows监听器:

 1 import java.awt.event.WindowAdapter;
 2 import java.awt.event.WindowEvent;
 3 import java.awt.event.WindowListener;
 4 import javax.swing.JFrame;
 5 
 6 public class WindowAdapterFrame extends JFrame{
 7     public WindowAdapterFrame() {
 8         setTitle("窗体适配器");
 9         setSize(600, 400);
10         setResizable(false);
11         
12         WindowListener listener =new WindowAdapter() {
13             @Override
14             public void windowClosing(WindowEvent e) {
15                 dispose();
16             }
17         };
18         this.addWindowListener(listener);
19     }
20     public static void main(String[] args) {
21         JFrame frame=new WindowAdapterFrame();
22         frame.setVisible(true);
23     }
24 }

3、通过实现MouseListener、MouseMotionListener接口来实现Mouse监听器:

 1 import java.awt.BorderLayout;
 2 import java.awt.Container;
 3 import java.awt.event.MouseEvent;
 4 import java.awt.event.MouseListener;
 5 import java.awt.event.MouseMotionListener;
 6 
 7 import javax.swing.JFrame;
 8 import javax.swing.JLabel;
 9 
10 public class MouseListenerFrame extends JFrame implements MouseListener, MouseMotionListener{
11     JLabel lblStatus=null;
12     public MouseListenerFrame() {
13         setTitle("MouseListener");
14         setSize(600, 400);
15         setDefaultCloseOperation(EXIT_ON_CLOSE);
16         setResizable(false);
17         
18         Container container =this.getContentPane();
19         lblStatus=new JLabel();
20         container.add(BorderLayout.SOUTH, lblStatus);
21         
22         container.addMouseListener(this);
23         container.addMouseMotionListener(this);
24     }
25     /**
26      *  值得注意的是String.format()方法的调用格式,是用","来隔开各个参数
27      */
28     @Override
29     public void mouseClicked(MouseEvent event) {
30         String msg=String.format("鼠标点击[%d, %d]",event.getX(),event.getY());
31         lblStatus.setText(msg);
32     }
33     @Override
34     public void mousePressed(MouseEvent e) {
35         String button=null;
36         if(MouseEvent.BUTTON1==e.getButton()) {
37             button="左键";
38         }else if(MouseEvent.BUTTON2==e.getButton()) {
39             button="滚轮";
40         }else if(MouseEvent.BUTTON3==e.getButton()) {
41             button="右键";
42         }
43         String msg=String.format("鼠标 %s 按下[%d,%d]",button,e.getX(),e.getY());
44         lblStatus.setText(msg);        
45     }
46     @Override
47     public void mouseReleased(MouseEvent e) {
48         String msg=String.format("鼠标释放[%d,%d]",e.getX(),e.getY());
49         lblStatus.setText(msg);
50     }
51     @Override
52     public void mouseEntered(MouseEvent e) {
53         String msg=String.format("鼠标进入[%d,%d]",e.getX(),e.getY());
54         lblStatus.setText(msg);
55     }
56     @Override
57     public void mouseExited(MouseEvent e) {
58         String msg=String.format("鼠标退出[%d,%d]",e.getX(),e.getY());
59         lblStatus.setText(msg);
60     }
61     @Override
62     public void mouseDragged(MouseEvent e) {
63         String msg=String.format("鼠标拖动,当前坐标 [%d,%d]",e.getX(),e.getY());
64         lblStatus.setText(msg);
65     }
66     @Override
67     public void mouseMoved(MouseEvent e) {
68         String msg=String.format("鼠标移动,当前坐标 [%d,%d]",e.getX(),e.getY());
69         lblStatus.setText(msg);
70     }
71     public static void main(String[] args) {
72         JFrame frame=new MouseListenerFrame();
73         frame.setVisible(true);
74     }
75 }

 4、通过实现KeyListener接口来实现Key监听器:

 1 import java.awt.BorderLayout;
 2 import java.awt.Container;
 3 import java.awt.event.KeyEvent;
 4 import java.awt.event.KeyListener;
 5 
 6 import javax.swing.JFrame;
 7 import javax.swing.JLabel;
 8 import javax.swing.JTextArea;
 9 
10 public class KeyListenerFrame extends JFrame implements KeyListener {
11     // 1. 属性
12     JLabel lblStatus = null;    
13 
14     // 2. 方法
15     public KeyListenerFrame() {
16         this.setTitle("鼠标事件");
17         setSize(600, 400);
18         setResizable(false);
19         setLocationRelativeTo(null);
20         setDefaultCloseOperation(EXIT_ON_CLOSE);
21 
22         Container contentPane = this.getContentPane();
23 
24         lblStatus = new JLabel();
25         contentPane.add(BorderLayout.SOUTH, lblStatus);
26         
27         JTextArea txaEdit = new JTextArea();
28         contentPane.add(BorderLayout.CENTER, txaEdit);
29 
30         txaEdit.addKeyListener(this);
31     }
32 
33     public static void main(String[] args) {
34         JFrame frame = new KeyListenerFrame();
35         frame.setVisible(true);
36     }
37 
38     @Override
39     public void keyPressed(KeyEvent event) {
40         lblStatus.setText("键盘按下: " + event.getKeyChar() + "(" + event.getKeyCode() + ")");
41     }
42 
43     @Override
44     public void keyReleased(KeyEvent event) {
45         lblStatus.setText("键盘放开: " + event.getKeyChar() + "(" + event.getKeyCode() + ")");
46     }
47 
48     @Override
49     public void keyTyped(KeyEvent event) {
50         lblStatus.setText("键盘敲击: " + event.getKeyChar() + "(" + event.getKeyCode() + ")");
51     }
52 }

 

以上是关于Java实现WindowsMouse监听器的主要内容,如果未能解决你的问题,请参考以下文章

从父片段到选项卡片段的接口侦听器不起作用

JavaFX窗口拖动

在同一个片段中实现多个事件监听器 - Android

listview OnItemClick 侦听器在片段中不起作用

在Google Maps android上放置点击监听器

如何使用事件侦听器来加载动画片段的循环