如何编写鼠标 - 运动监听器

原文: https://docs.oracle.com/javase/tutorial/uiswing/events/mousemotionlistener.html

当用户使用鼠标(或类似的输入设备)移动屏幕上的光标时,鼠标移动事件会发出通知。有关侦听其他类型鼠标事件(如点击)的信息,请参阅如何编写鼠标监听器。有关侦听鼠标滚轮事件的信息,请参阅如何编写鼠标滚轮监听器

如果应用程序需要检测鼠标事件和鼠标移动事件,请使用 MouseInputAdapter 类,它实现 MouseInputListener 一个方便的接口,实现MouseListener ]和MouseMotionListener接口。

或者,使用实现MouseMotionListener接口的相应 MouseAdapter AWT 类创建MouseMotionEvent并覆盖特定事件的方法。

以下演示代码包含鼠标移动监听器。此演示与如何编写鼠标监听器部分中描述的演示完全相同,只是将MouseMotionListener接口替换为MouseListener接口。此外,MouseMotionEventDemo 实现mouseDraggedmouseMoved方法而不是鼠标监听器方法,并显示坐标而不是点击次数。

MouseMotionEventDemo screen shot


Try this:

  1. 单击“启动”按钮以使用 Java™Web Start下载 JDK 7 或更高版本)运行 MouseMotionEventDemo。或者,要自己编译并运行示例,请参考示例索引Launches the MouseMotionEventDemo application

  2. 将光标移动到窗口顶部的黄色矩形中。 您将看到一个或多个鼠标移动的事件。

  3. 按住鼠标按钮,然后移动鼠标,使光标位于黄色矩形之外。 你会看到鼠标拖动的事件。

您可以在 MouseMotionEventDemo.javaBlankArea.java 中找到演示代码。 MouseMotionEventDemo的以下代码片段实现了鼠标移动事件处理:

  1. public class MouseMotionEventDemo extends JPanel
  2. implements MouseMotionListener {
  3. //...in initialization code:
  4. //Register for mouse events on blankArea and panel.
  5. blankArea.addMouseMotionListener(this);
  6. addMouseMotionListener(this);
  7. ...
  8. }
  9. public void mouseMoved(MouseEvent e) {
  10. saySomething("Mouse moved", e);
  11. }
  12. public void mouseDragged(MouseEvent e) {
  13. saySomething("Mouse dragged", e);
  14. }
  15. void saySomething(String eventDescription, MouseEvent e) {
  16. textArea.append(eventDescription
  17. + " (" + e.getX() + "," + e.getY() + ")"
  18. + " detected on "
  19. + e.getComponent().getClass().getName()
  20. + newline);
  21. }
  22. }

SelectionDemo 示例绘制一个矩形,说明用户当前的拖动。要绘制矩形,应用程序必须为三种鼠标事件实现事件处理器:鼠标按下,鼠标拖动和鼠标释放。要获知所有这些事件,处理器必须同时实现MouseListenerMouseMotionListener接口,并注册为鼠标监听器和鼠标移动监听器。为了避免必须定义空方法,处理器不直接实现任何一个监听器接口。相反,它扩展了MouseInputAdapter,如下面的代码片段所示。

  1. ...//where initialization occurs:
  2. MyListener myListener = new MyListener();
  3. addMouseListener(myListener);
  4. addMouseMotionListener(myListener);
  5. ...
  6. private class MyListener extends MouseInputAdapter {
  7. public void mousePressed(MouseEvent e) {
  8. int x = e.getX();
  9. int y = e.getY();
  10. currentRect = new Rectangle(x, y, 0, 0);
  11. updateDrawableRect(getWidth(), getHeight());
  12. repaint();
  13. }
  14. public void mouseDragged(MouseEvent e) {
  15. updateSize(e);
  16. }
  17. public void mouseReleased(MouseEvent e) {
  18. updateSize(e);
  19. }
  20. void updateSize(MouseEvent e) {
  21. int x = e.getX();
  22. int y = e.getY();
  23. currentRect.setSize(x - currentRect.x,
  24. y - currentRect.y);
  25. updateDrawableRect(getWidth(), getHeight());
  26. Rectangle totalRepaint = rectToDraw.union(previouseRectDrawn);
  27. repaint(totalRepaint.x, totalRepaint.y,
  28. totalRepaint.width, totalRepaint.height);
  29. }
  30. }

相应的适配器类别为 MouseMotionAdapterMouseAdapter

方法 目的
mouseDragged(MouseEvent) 响应用户在按住鼠标按钮的同时移动鼠标而调用。触发最近鼠标按下的事件的组件触发此事件,即使光标不再在该组件上。
mouseMoved(MouseEvent) 响应用户在没有按下鼠标按钮的情况下移动鼠标而调用。此事件由当前在光标下的组件触发。

每个鼠标运动事件方法都有一个参数 - 而而不是称为MouseMotionEvent!相反,每个鼠标移动事件方法都使用MouseEvent参数。有关使用MouseEvent对象的信息,请参阅 MouseEvent API

下表列出了使用鼠标移动监听器的示例。

在哪里描述 笔记
MouseMotionEventDemo 这个部分 报告在空白面板中发生的所有鼠标移动事件,以演示触发鼠标移动事件的环境。
LayeredPaneDemo

LayeredPaneDemo2 | 如何使用分层窗格 | 在分层窗格内移动 Duke 图像以响应鼠标移动事件。 | | SelectionDemo | | 允许用户拖动矩形以选择图像的一部分。使用MouseInputAdapter的子类来监听鼠标事件和鼠标移动事件。 | | GlassPaneDemo | 如何使用根窗格 | 使用MouseInputAdapter的子类在根窗格的玻璃窗格上侦听鼠标事件和鼠标移动事件。将事件重新分配到基础组件。 | | ScrollDemo | 如何使用滚动窗格 | 标签子类 ScrollablePicture 使用鼠标移动监听器,即使用户将光标拖到窗口外,也允许用户滚动图片。 |