how are the appropriate methods of MouseMotionListener in Java Swing?

不羁岁月 提交于 2019-12-12 02:35:38

问题


consider this class:

public class mycomponent extends JComponent {

     public mycomponent(){
       addMouseMotionListener(new MouseMotionHandler());
     }


     class MouseMotionHandler implements MouseMotionListener{
          public void mouseMoved(MouseEvent event){
           //do something
          }

          public void mouseDragged(MouseEvent event){
           //do something
          }
     }
}

Now Lets say a mouse drag event occurs. How does the MouseMotionHandler knows which method to call. of the two methods implemented. Or how is the method to be called resolved in run-time when an event occurs.

If the MouseEvent event that gets passed to these method is MouseDrag Event, how is that only mouseDragged is called.

and how does it know that it is a MouseDrag event and not MouseMove event?


回答1:


The long and short of it...

The AWT core starts a native "event loop". This loop basically takes in events from the OS and processes them. If the event is of interest to the current application context, the event is processed and added to the Event Queue.

The Event Queue is processed by the Event Dispatching Thread, which dispatches the event to the appropriate listener, based on who the event was for.

This is a significant simplification of the process.

None-the-less, when an event comes into the native "event loop", it's properties are inspected and an appropriate AWT Event is generated. How this is determined comes down a lot to how the OS passes it's event information, but basically, if the OS detects a drag, the MouseEvent has it's ID property set to MouseEvent.MOSUE_DRAGGED, this allows the Component to sift through the events and determine the best listener it should notify, which comes out to be MouseMotionListener.mouseDragged(MouseEvent)

For example, this is the processMouseMotionEvent method take from Component

protected void processMouseMotionEvent(MouseEvent e) {
    MouseMotionListener listener = mouseMotionListener;
    if (listener != null) {
        int id = e.getID();
        switch(id) {
          case MouseEvent.MOUSE_MOVED:
              listener.mouseMoved(e);
              break;
          case MouseEvent.MOUSE_DRAGGED:
              listener.mouseDragged(e);
              break;
        }
    }
}



回答2:


mouseDragged and mouseMoved events are different in terms of whether mouse button is pressed or not. Here is the description of both the methods:

mouseDragged(MouseEvent)
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)
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.

Here is an excellent tutorial about handling mouse events:

http://docs.oracle.com/javase/tutorial/uiswing/events/mousemotionlistener.html



来源:https://stackoverflow.com/questions/21106626/how-are-the-appropriate-methods-of-mousemotionlistener-in-java-swing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!