如何编写鼠标动作侦听器

鼠标移动事件通知用户何时使用鼠标(或类似的 Importing 设备)移动屏幕光标。有关侦听其他类型的鼠标事件(如单击)的信息,请参见如何编写鼠标侦听器。有关监听鼠标滚轮事件的信息,请参见如何编写鼠标滚轮监听器

如果应用程序需要同时检测鼠标事件和鼠标移动事件,请使用MouseInputAdapter类,该类实现MouseInputListener,该interface方便地实现MouseListenerMouseMotionListenerinterface。

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

以下演示代码包含一个鼠标移动侦听器。该演示与如何编写鼠标侦听器部分中描述的演示完全相同,只是用MouseMotionListenerinterface代替MouseListenerinterface。另外,MouseMotionEventDemo 实现mouseDraggedmouseMoved方法而不是鼠标侦听器方法,并显示坐标而不是单击数。

MouseMotionEventDemo 屏幕截图

Try this:

  • 单击启动按钮以使用Java™Web 开始(下载 JDK 7 或更高版本)运行 MouseMotionEventDemo。另外,要自己编译和运行示例,请查阅example index

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

  • 按住鼠标按钮,然后移动鼠标,使光标位于黄色矩形的外部。
    您将看到鼠标拖动事件。

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

public class MouseMotionEventDemo extends JPanel 
                                  implements MouseMotionListener {
    //...in initialization code:
        //Register for mouse events on blankArea and panel.
        blankArea.addMouseMotionListener(this);
        addMouseMotionListener(this);
        ...
    }

    public void mouseMoved(MouseEvent e) {
       saySomething("Mouse moved", e);
    }

    public void mouseDragged(MouseEvent e) {
       saySomething("Mouse dragged", e);
    }

    void saySomething(String eventDescription, MouseEvent e) {
        textArea.append(eventDescription 
                        + " (" + e.getX() + "," + e.getY() + ")"
                        + " detected on "
                        + e.getComponent().getClass().getName()
                        + newline);
    }
}

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

...//where initialization occurs:
    MyListener myListener = new MyListener();
    addMouseListener(myListener);
    addMouseMotionListener(myListener);
...
private class MyListener extends MouseInputAdapter {
    public void mousePressed(MouseEvent e) {
        int x = e.getX();
        int y = e.getY();
        currentRect = new Rectangle(x, y, 0, 0);
        updateDrawableRect(getWidth(), getHeight());
        repaint();
    }

    public void mouseDragged(MouseEvent e) {
        updateSize(e);
    }

    public void mouseReleased(MouseEvent e) {
        updateSize(e);
    }

    void updateSize(MouseEvent e) {
        int x = e.getX();
        int y = e.getY();
        currentRect.setSize(x - currentRect.x,
                            y - currentRect.y);
        updateDrawableRect(getWidth(), getHeight());
        Rectangle totalRepaint = rectToDraw.union(previouseRectDrawn); 
        repaint(totalRepaint.x, totalRepaint.y,
                totalRepaint.width, totalRepaint.height);
    }
}

鼠标移动监听器 API

MouseMotionListener interface

相应的适配器类为MouseMotionAdapterMouseAdapter.

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

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

使用鼠标移动侦听器的示例

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

ExampleWhere DescribedNotes
MouseMotionEventDemoThis section报告在空白面板中发生的所有鼠标移动事件,以演示触发鼠标移动事件的情况。
LayeredPaneDemo and

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