假设想对一个鼠标的操作进行监听,假设鼠标按下、松开等。则能够使用MouseListener接口。
package com.beyole.util; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextArea; class MyMouseHandle extends JFrame implements MouseListener { private JTextArea text = new JTextArea(); public MyMouseHandle() { super.setTitle("Crystal");// 设置标题 JScrollPane pane = new JScrollPane(text);// 增加滚动栏 pane.setBounds(5, 5, 300, 200);// 设置绝对位置 super.add(pane);// 向窗口中增加组件 text.addMouseListener(this);// 增加mouse监听 super.setSize(310, 210); super.setVisible(true); super.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent arg0) { System.exit(1); } }); } public void mouseClicked(MouseEvent e)// 鼠标单击事件 { int c = e.getButton();// 得到按下的鼠标键 String mouseInfo = null;// 接收信息 if (c == MouseEvent.BUTTON1)// 推断是鼠标左键按下 { mouseInfo = "左键"; } else if (c == MouseEvent.BUTTON3) {// 推断是鼠标右键按下 mouseInfo = "右键"; } else { mouseInfo = "滚轴"; } text.append("鼠标单击:" + mouseInfo + ". "); } public void mouseEntered(MouseEvent e)// 鼠标进入组件 { text.append("鼠标进入组件. "); } public void mouseExited(MouseEvent e)// 鼠标退出组件 { text.append("鼠标退出组件. "); } public void mousePressed(MouseEvent e)// 鼠标按下 { text.append("鼠标按下. "); } public void mouseReleased(MouseEvent e)// 鼠标松开 { text.append("鼠标松开. "); } } public class MyMouseEventDemo { public static void main(String[] args) { new MyMouseHandle(); } }
程序截图: