MouseListener on JFrame

倾然丶 夕夏残阳落幕 提交于 2019-11-29 14:16:56

You can get all events and check if their source is a component in the JFrame.

See Toolkit.addAWTEventListener

There is an invisible component that overlays the whole GUI, the "glass pane". You can attach your listeners to that. Example:

JFrame frame = new JFrame();
Component glassPane = frame.getGlassPane();
glassPane.addMouseListener(myListener);

If you want your intercepted events to pass through to the underlying components, you can redispatch them. For example:

public void mouseMoved(MouseEvent e) {
    redispatchMouseEvent(e, false);
}

Because the contents ( probably a JPanel ) are "shadowing" and consuming the events and they don't reach the JFrame.

What you can do is to add the same listener to all the children. There should be a better way though.

An alternative to AWTEventListener is to push an EventQueue. This has the advantage that applets and WebStart application can do this.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!