java 为什么在使用 MouseAdapter 时没有收到 mouseDragged 事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5577619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why are mouseDragged-events not received when using MouseAdapter?
提问by Tim
Why are mouseDragged
-events only received when using MouseMotionAdapter
and not when using MouseAdapter
?
为什么mouseDragged
-events 只在使用MouseMotionAdapter
时接收,而在使用时不接收MouseAdapter
?
Java has two abstract adapter classes for receiving mouse-events ;MouseAdapter
and MouseMotionAdapter
.
Java 有两个抽象适配器类用于接收鼠标事件;MouseAdapter
和MouseMotionAdapter
。
Both classes have a mouseDragged(MouseEvent e)
-method, but the
one in MouseAdapter
does not seem to work ; mouseDragged
-events
never get through with this one.
两个类都有一个mouseDragged(MouseEvent e)
-method,但
一个 inMouseAdapter
似乎不起作用;mouseDragged
- 事件
永远无法解决这个问题。
Both classes implement the MouseMotionListener
-interface which
defines the mouseDragged
-event, so I don't understand why it is
not working correctly on both of them.
这两个类都实现了MouseMotionListener
其γ-接口
定义了mouseDragged
-event,所以我不明白为什么它
不能在他们两人的正常工作。
Here is sample-code which shows this issue :
这是显示此问题的示例代码:
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JFrame;
public class SwingApp extends JFrame
{
public SwingApp()
{
// No mouseDragged-event is received when using this :
this.addMouseListener(new mouseEventHandler());
// This works correct (when uncommented, of course) :
// this.addMouseMotionListener(new mouseMovedEventHandler());
setBounds(400,200, 550,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
}
public static void main(String args[])
{
new SwingApp();
}
class mouseEventHandler extends MouseAdapter
{
@Override
public void mouseDragged(MouseEvent e) // Why is this method never called ?
{
System.out.println(String.format("MouseDragged via MouseAdapter / X,Y : %s,%s ", e.getX(), e.getY()));
}
}
class mouseMovedEventHandler extends MouseMotionAdapter
{
@Override
public void mouseDragged(MouseEvent e)
{
System.out.println(String.format("MouseDragged via MouseMotionAdapter / X,Y : %s,%s ", e.getX(), e.getY()));
}
}
}
回答by aioobe
If you add it through
如果你通过添加它
this.addMouseListener(new mouseEventHandler());
you will not receive motion related MouseEvents
(That's not what you registered the listener for!)
你不会收到相关的动作MouseEvents
(那不是你注册监听器的目的!)
You'll have to add the listener twice, i.e., add it using addMouseMotionListener
as well:
您必须添加两次侦听器,即也使用以下addMouseMotionListener
方法添加它:
mouseEventHandler handler = new mouseEventHandler();
this.addMouseListener(handler);
this.addMouseMotionListener(handler);
in order to get both type of events.
为了获得这两种类型的事件。
(A side node, always use a capital first letter for your classes, i.e., use MouseEventHandler
instead :-)
(一个侧节点,总是为你的类使用大写的第一个字母,即使用MouseEventHandler
:-)
回答by iluxa
you gotta add your MouseAdapter as both mouseListener and mouseMotionListener, and you'll be golden. MouseAdapter implements both MouseListener and MouseMotionListener, but your component doesn't know to pass mouseDragged events to it unless you call addMouseMotionListener
你必须将你的 MouseAdapter 添加为 mouseListener 和 mouseMotionListener,你会很高兴。MouseAdapter 实现了 MouseListener 和 MouseMotionListener,但是除非您调用 addMouseMotionListener,否则您的组件不知道将 mouseDragged 事件传递给它
回答by jvn91173
The top answers for this question are now pretty old. For anyone using Java JDK 8 or later, be sure to check out https://docs.oracle.com/javase/tutorial/uiswing/events/mousemotionlistener.html.
这个问题的最佳答案现在已经很老了。对于使用 Java JDK 8 或更高版本的任何人,请务必查看https://docs.oracle.com/javase/tutorial/uiswing/events/mousemotionlistener.html。
To summarize, your listener class has to extend from MouseInputAdapter
instead of MouseMotionAdapter
or MouseAdapter
. You'll add your listener class using addMouseMotionListener
and addMouseListener
as follows:
总而言之,您的侦听器类必须从而MouseInputAdapter
不是MouseMotionAdapter
or扩展MouseAdapter
。您将使用addMouseMotionListener
和添加您的侦听器类addMouseListener
,如下所示:
MyMouseHandler myMouseHandler = new MyMouseHandler ();
addMouseMotionListener(myMouseHandler);
addMouseListener(myMouseHandler);