【问题标题】:Difference between mouseListener and mouseMotionListener in Java?Java中mouseListener和mouseMotionListener的区别?
【发布时间】:2010-12-20 09:26:16
【问题描述】:

mouseMotionListener 是否会在鼠标移到组件上时触发事件,而 mouseListener 只有在我按下按钮时才会触发?

所以如果我只有一个 mousePressed 事件,那么我不需要 mouseMotionListener 吗?只有当我有 mouseEntered 或 mouseExited 时?

【问题讨论】:

    标签: java user-interface mouse


    【解决方案1】:

    是的,你是对的。 mouseMotionListener 用于在鼠标移到“热点”上时执行操作

    很好的例子可以找到here

    在处理mousePressed 事件时,您只需要mousePressed 事件,除非您想在鼠标悬停时添加更多要执行的事件。

    【讨论】:

      【解决方案2】:

      他们监听不同的事件:

      MouseListener

      mouseClicked(MouseEvent event)   // Called just after the user clicks the listened-to component.
      mouseEntered(MouseEvent event)   // Called just after the cursor enters the bounds of the listened-to component.
      mouseExited(MouseEvent event)    // Called just after the cursor exits the bounds of the listened-to component.
      mousePressed(MouseEvent event)   // Called just after the user presses a mouse button while the cursor is over the listened-to component.
      mouseReleased(MouseEvent event)  // Called just after the user releases a mouse button after a mouse press over the listened-to component
      

      MouseMotionListener

      mouseDragged(MouseEvent event)   // Called in response to the user moving the mouse while holding a mouse button down. This event is fired by the component that fired the most recent mouse-pressed event, even if the cursor is no longer over that component.
      mouseMoved(MouseEvent event)     // Called in response to the user moving the mouse with no mouse buttons pressed. This event is fired by the component that's currently under the cursor.
      

      根据你所关注的事件添加监听器。

      【讨论】:

        猜你喜欢
        • 2010-11-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多