摆动焦点问题
在我的应用程序中,我有一个带有工具栏的框架(工具栏包含一些操作)。 我希望工具栏仅在窗口聚焦时才可见。 所以,我在窗口上注册了一个windowFocusListener。 问题是—— 当窗口未聚焦并且我单击工具栏操作应在的位置时,将执行该操作。 发生这种情况是因为 WindowFocusGained 在释放鼠标按钮之前被调用,而当释放鼠标按钮时它调用 actionPerformed。
有人知道解决这个问题的方法吗? 有谁知道如何确定现在是否单击了鼠标按钮?
In my application I have a frame, with toolbar (the toolbar contains some actions).
I want the toolbar to be visible only when the window is focused.
So, I registered a windowFocusListener on the window.
The problem is-
when the window is not focused and I click on the place where a tool bar action should be- the action is performed.
This happens because the WindowFocusGained is called before the mouse button is released and when the mouse button released it calls the actionPerformed.
Does anybody has any idea for a work around for this problem?
Does anybody know how to determine wether the mouse button is clicked now?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将 MouseListener 添加到窗口,并检查工具栏在 mousePressed 事件中是否可见。如果按下鼠标时工具栏不可见,请在工具栏上设置一个标志(例如“ignoreNextAction”)。
在工具栏中,检查 actionPerformed 事件处理程序中的该标志。
在窗口的 mouseReleased 事件中重置工具栏标志,以便下次单击能够正常工作。
这是假设窗口上的 mouseReleased 事件发生在工具栏上的 actionPerformed 之后(对此不确定)。最坏的情况是,您可以使用一个计时器,该计时器会在鼠标事件发生 50 毫秒后重置标志。
You could add a MouseListener to the window, and check if the toolbar is visible in the mousePressed event. If the toolbar is not visible at the time the mouse is pressed, set a flag on the toolbar (something like "ignoreNextAction").
In the toolbar, check that flag in your actionPerformed event handler.
Reset the toolbar flag in the mouseReleased event on the window, so that the next click will work correctly.
This is assuming the mouseReleased event on the window happens after the actionPerformed on the toolbar (not sure about this). Worst case, you can work with a timer which resets the flag 50ms after the mouse event.
现在,您只能在窗口失去焦点时隐藏工具栏,也可以使用
setEnabled(false)
禁用所有按钮,然后在获得焦点时重新启用。或者,将 setEnabled(..) 与按钮的可见性(而不是窗口焦点)同步。You could now only hide the toolbar when the window loses focus, but also disable all buttons with
setEnabled(false)
, then re-enable on focus gain. Alternatively, synchronize setEnabled(..) with the visibility of the buttons (instead of the window focus).