如何检测用户是否点击了窗口标题栏或 Java 中窗口边框的任何其他部分 [英] How to detect if user clicks on window title bar or any other part of window border in Java

查看:28
本文介绍了如何检测用户是否点击了窗口标题栏或 Java 中窗口边框的任何其他部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Swing Popup 类实现了一个弹出式数字小键盘.我有一个与 JTextField 关联的按钮,当用户单击它时会打开数字小键盘,然后当/如果 JTextField 失去焦点时,弹出窗口将关闭.它通常运行良好,除了偶尔我会在弹出窗口隐藏后从弹出窗口中遗留"出来的工件.有时,工件是显示在弹出窗口中的组件的图像,但更多情况下,它是一种黑洞",它掩盖了弹出窗口所在的屏幕同一区域中显示的任何其他内容,这只能通过关闭应用程序和 JVM 来补救.

I have implemented a pop-up numeric keypad using the Swing Popup class. I have a button associated with a JTextField that opens the numeric keypad when the user clicks on it, and then when/if the JTextField loses focus the Popup closes. It generally works well, except that occasionally I get artifacts that are "left over" from a Popup after it has been hidden. Sometimes the artifact is an image of the Components that were shown in the Popup, but more often it is a "black hole" of sorts that obscures anything else displayed in the same area of the screen in which the Popup had been, which can only be remedied by shutting down the application and the JVM.

这个问题很难重现,但是当用户在弹出窗口打开时操作基本窗口时,它似乎会表现出来,例如通过移动或调整它的大小.我的想法是在发生类似情况时简单地隐藏 Popup,我可以通过 WindowListener 和 ComponentListener 的组合来实现.但是,我想更进一步,只要用户点击窗口标题栏或其框架的其他部分,甚至在他们移动、调整大小或图标化之前,就立即隐藏弹出窗口.JComboBox 弹出窗口实际上是这样工作的.但是,我一直找不到任何一种机制可以通知我用户单击了窗口标题栏.我已经查看了 JComboBox 和相关代码,但也无法从中弄清楚它是如何发挥这种魔力的.我可以使用其他类型的侦听器来获取此类通知吗?

The problem is difficult to reproduce, but it seems to manifest when the user manipulates the base Window while the Popup is open, such as by moving or resizing it. My thought is to simply hide the Popup when anything like that happens, which I can do with a combination of a WindowListener and a ComponentListener. However, I'd like to take it one step further and hide the Popup as soon as the user so much as clicks on the window title bar or another portion of its frame, even before they move, resize or iconify it. JComboBox popups in fact work this way. However, I have been unable to find any kind of mechanism by which I can be notified that a user has clicked on the window title bar. I've looked at the JComboBox and related code and can't figure out from that how it's working this magic, either. Is there any other kind of listener I could use to get this kind of notification?

推荐答案

我使用 Swing Popup 类实现了一个弹出式数字小键盘.

I have implemented a pop-up numeric keypad using the Swing Popup class.

当您发布问题时,请发布您的代码来演示实现和问题.

Well post your code demonstrating the implementation and problem when you post a question.

我不知道你在做什么,但你可以使用 JPopupMenu.当您单击没有 FocusListener 或任何其他逻辑的框架标题栏时,这将关闭.

I don't know exactly what you are doing but you might be able to use a JPopupMenu. This will close when you click on the frame title bar with no FocusListener or any additional logic.

这篇关于如何检测用户是否点击了窗口标题栏或 Java 中窗口边框的任何其他部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆