我在我的MouseMotionListener中添加了一个jframe来控制来自JFrame中所有对象的所有鼠标运动消息,但是当我移动鼠标在JLayeredPane上时,不会产生任何消息。请帮助我在我的JFrame中添加一个中央JFrame,以控制来自其中所有对象的所有消息。
非常感谢。
发布于 2016-05-27 15:06:11
您可能希望使用AWTEventListener来侦听所有AWT消息。
下面的代码显示了如何侦听鼠标和键事件:
long eventMask = AWTEvent.MOUSE_MOTION_EVENT_MASK
+ AWTEvent.MOUSE_EVENT_MASK
+ AWTEvent.KEY_EVENT_MASK;
Toolkit.getDefaultToolkit().addAWTEventListener( new AWTEventListener()
{
public void eventDispatched(AWTEvent e)
{
System.out.println(e.getID());
}
}, eventMask);有关详细信息,请参阅全局事件侦听器。
发布于 2016-05-27 14:23:54
下面是递归地向所有组件添加一个MouseMotionListener的代码。请注意,为了处理生成的MouseEvents,您需要使用SwingUtilities将点从特定组件的空间转换为JFrame空间。
public static void installMouseMotionListenerOnAll(Component c, MouseMotionListener mml) {
c.addMouseMotionListener(mml);
if (c instanceof Container) {
for (Component child : ((Container)c).getComponents()) {
installMouseMotionListenerOnAll(child, mml);
}
}
}https://stackoverflow.com/questions/37485677
复制相似问题