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?

有帮助吗?

解决方案

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;
        }
    }
}

其他提示

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top