Java Swing 中 MouseMotionListener 的适当方法如何? [英] how are the appropriate methods of MouseMotionListener in Java Swing?

查看:19
本文介绍了Java Swing 中 MouseMotionListener 的适当方法如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这个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
          }
     }
}

现在假设发生了 鼠标拖动事件.MouseMotionHandler 如何知道调用哪个方法.实现的两种方法.或者当 event 发生时,如何在运行时解析要调用的方法.

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.

如果传递给这些方法的 MouseEvent 事件MouseDrag Event,那么如何只调用 mouseDragged.

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

它怎么知道这是一个 MouseDrag 事件而不是 MouseMove 事件?

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

推荐答案

长短不一...

AWT 核心启动一个原生的事件循环".这个循环基本上从操作系统接收事件并处理它们.如果当前应用程序上下文对事件感兴趣,则会处理该事件并将其添加到事件队列中.

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.

尽管如此,当一个事件进入本机事件循环"时,它的属性会被检查并生成一个适当的 AWT 事件.如何确定这一点很大程度上取决于操作系统如何传递其事件信息,但基本上,如果操作系统检测到拖动,MouseEvent 会将其 ID 属性设置为 MouseEvent.MOSUE_DRAGGED,这允许 Component 筛选事件并确定它应该通知的最佳侦听器,结果是 MouseMotionListener.mouseDragged(MouseEvent)

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)

例如,这是取自 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;
        }
    }
}

这篇关于Java Swing 中 MouseMotionListener 的适当方法如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆