Java 中的 MouseListener 和 MouseMotionListener 有什么区别?
awtjavaobject oriented programmingprogrammingswing
我们可以在鼠标稳定时实现 MouseListener 接口来处理鼠标事件,而在鼠标移动时处理鼠标事件,我们可以实现 MouseMotionListener 接口。
鼠标监听器
当我们在源对象上按下、释放或单击(按下后释放)鼠标按钮(左键或右键)或将鼠标指针定位在(进入)和远离(退出)源对象时,将触发 MouseEvent。
MouseListener 接口声明了以下五个抽象方法
语法
public void mouseClicked(MouseEvent evt) public void mousePressed(MouseEvent evt) public void mouseReleased(MouseEvent evt) public void mouseEntered(MouseEvent evt) public void mouseExited(MouseEvent evt)
示例
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MouseListenerTest extends JFrame implements MouseListener { private JTextField tfMouseX, tfMouseY; public MouseListenerTest() { setLayout(new FlowLayout()); add(new Label("X-Click: ")); tfMouseX = new JTextField(10); tfMouseX.setEditable(false); add(tfMouseX); add(new Label("Y-Click: ")); tfMouseY = new JTextField(10); tfMouseY.setEditable(false); add(tfMouseY); addMouseListener(this); setTitle("MouseListener Test"); setLocationRelativeTo(null); setSize(350, 100); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public static void main(String[] args) { new MouseListenerTest(); } @Override public void mouseClicked(MouseEvent evt) { tfMouseX.setText(evt.getX() + ""); tfMouseY.setText(evt.getY() + ""); } // Need to provide an empty body to compile. @Override public void mousePressed(MouseEvent evt) { } @Override public void mouseReleased(MouseEvent evt) { } @Override public void mouseEntered(MouseEvent evt) { } @Override public void mouseExited(MouseEvent evt) { } }
输出
MouseMotionListener
当我们移动和拖动源对象上的鼠标指针时,也会触发MouseEvent。但我们需要使用MouseMotionListener来处理鼠标移动和鼠标拖动。
MouseMotionListener接口声明了以下两个抽象方法
语法
public void mouseDragged(MouseEvent e) public void mouseMoved(MouseEvent e)
示例
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MouseMotionListenerTest extends JFrame implements MouseListener, MouseMotionListener { private JTextField tfMouseClickX, tfMouseClickY; private JTextField tfMousePositionX, tfMousePositionY; public MouseMotionListenerTest() { setLayout(new FlowLayout()); add(new Label("X-Click: ")); tfMouseClickX = new JTextField(10); tfMouseClickX.setEditable(false); add(tfMouseClickX); add(new Label("Y-Click: ")); tfMouseClickY = new JTextField(10); tfMouseClickY.setEditable(false); add(tfMouseClickY); add(new JLabel("X-Position: ")); tfMousePositionX = new JTextField(10); tfMousePositionX.setEditable(false); add(tfMousePositionX); add(new JLabel("Y-Position: ")); tfMousePositionY = new JTextField(10); tfMousePositionY.setEditable(false); add(tfMousePositionY); addMouseListener(this); addMouseMotionListener(this); setTitle("MouseMotionListener Test"); setSize(400, 120); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public static void main(String[] args) { new MouseMotionListenerTest(); } @Override public void mouseClicked(MouseEvent evt) { tfMouseClickX.setText(evt.getX() + ""); tfMouseClickY.setText(evt.getY() + ""); } // 需要提供一个空的主体以便编译 @Override public void mousePressed(MouseEvent evt) { } @Override public void mouseReleased(MouseEvent evt) { } @Override public void mouseEntered(MouseEvent evt) { } @Override public void mouseExited(MouseEvent evt) { } @Override public void mouseMoved(MouseEvent evt) { tfMousePositionX.setText(evt.getX() + ""); tfMousePositionY.setText(evt.getY() + ""); } // 需要提供一个空主体才能进行编译 @Override public void mouseDragged(MouseEvent evt) { } }