java 如何在 JFrame/Swing 中捕获所有鼠标事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1186333/
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
How can I capture all mouse events in a JFrame/Swing?
提问by Rob Mayhew
I have a JFrame that has a large number of changing child components. (Many layers) Is there any way to add a listener for all mouse events? Something like KeyEventDispatcher?
我有一个 JFrame,它有大量不断变化的子组件。(许多层)有没有办法为所有鼠标事件添加一个监听器?类似 KeyEventDispatcher 的东西?
回答by camickr
Use an AWTEventListener to filter out the MouseEvents:
使用 AWTEventListener 过滤掉 MouseEvents:
long eventMask = AWTEvent.MOUSE_MOTION_EVENT_MASK + AWTEvent.MOUSE_EVENT_MASK;
Toolkit.getDefaultToolkit().addAWTEventListener( new AWTEventListener()
{
public void eventDispatched(AWTEvent e)
{
System.out.println(e);
}
}, eventMask);
回答by Nate
You could add a GlassPaneover your entire JFrame, add a MouseInputAdapterto it to grab all possible mouse events, and then use [SwingUtilities.getDeepestComponentAt()][3] to get the actual component and [SwingUtilities.convertMouseEvent()][4] to delegate the mouse event from the glass pane to the actual component.
您可以在整个 JFrame 上添加一个GlassPane,向其中添加一个MouseInputAdapter以获取所有可能的鼠标事件,然后使用 [SwingUtilities.getDeepestComponentAt()][3] 获取实际组件和 [SwingUtilities.convertMouseEvent()][4] ] 将鼠标事件从玻璃窗格委托给实际组件。
However, I'm not sure of the performance impact of this - unlike KeyEventDispatcher, which just needs to fire an event whenever a key is pressed, multiple events are generated as the user moves the mouse - and unlike KeyEventDispatcher, you need to re-send the event to the lower component for it to handle it.
但是,我不确定这对性能的影响 - 与 KeyEventDispatcher 不同,它只需要在按下某个键时触发一个事件,当用户移动鼠标时会生成多个事件 - 与 KeyEventDispatcher 不同的是,您需要重新 -将事件发送到下层组件以供其处理。
(Sorry - stackoverflow isn't handling the links to the SwingUtilities methods correctly... links are showing below rather than in the text.)
(抱歉 - stackoverflow 没有正确处理指向 SwingUtilities 方法的链接……链接显示在下方而不是文本中。)
[3]: http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/SwingUtilities.html#getDeepestComponentAt(java.awt.Component, int, int) [4]: http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/SwingUtilities.html#convertMouseEvent(java.awt.Component, java.awt.event.MouseEvent, java.awt.Component)
[3]: http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/SwingUtilities.html#getDeepestComponentAt(java.awt.Component, int, int) [4]: http: //java.sun.com/j2se/1.4.2/docs/api/javax/swing/SwingUtilities.html#convertMouseEvent(java.awt.Component, java.awt.event.MouseEvent, java.awt.Component)
回答by m_vitaly
You have to use JFrame's glassPane: http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JFrame.html#getGlassPane()
你必须使用 JFrame 的 glassPane:http: //java.sun.com/j2se/1.4.2/docs/api/javax/swing/JFrame.html#getGlassPane()
Just get the glass pane of a JFrame with frm.getGlassPane() and use addMouseListener() on it to capture all mouse event inside the window.
只需使用 frm.getGlassPane() 获取 JFrame 的玻璃窗格,并在其上使用 addMouseListener() 来捕获窗口内的所有鼠标事件。
回答by akf
You might want to implement a subclass of MouseAdapter, an abstract class that provides empty implementations of all of the methods defined in the Mouse*ListenerInterfaces. Once you do that, you can register it with your child components as a MouseListenerwhen they are created. As you indicate that your components are 'changing,' you will want to make sure the you also unregister your listener if you hope to release your components during the lifecycle of your JFrame.
您可能想要实现 的子类MouseAdapter,这是一个抽象类,它提供Mouse*Listener接口中定义的所有方法的空实现。一旦你这样做了,你就可以在你的子组件中注册它作为MouseListener它们被创建时的一个。当您指出您的组件正在“更改”时,如果您希望在 JFrame 的生命周期期间发布您的组件,您将需要确保您也取消注册您的侦听器。
回答by Midhat
Implement all mouse-related listeners in a class, and register that class as the handler for all mouse related events
在一个类中实现所有与鼠标相关的侦听器,并将该类注册为所有鼠标相关事件的处理程序
Mouse Related interfaces would be
鼠标相关接口是
MouseListener MouseMotionListener MouseWheelListener
MouseListener MouseMotionListener MouseWheelListener

