如何捕获窗口最小化事件?
我想创建一个 JFrame
实例,然后单击其最小化按钮
,我想将其隐藏到系统托盘
,这通常是Windows 的任务栏
。
我开始知道,通过使用 java.awt 包中的 SystemTray 类,我可以做到这一点,但我没有得到任何关于它的教程,也没有任何工作程序示例。
我在这里问这个问题是为了获取 SystemTray 类的教程网站的链接,或者是否有人知道如何捕获窗口最小化事件(一个工作示例)。
I want to create a JFrame
instance and on the click of its minimize button
, I would like to hide it to the System Tray
which is usually the taskbar
of windows.
I'd come to know that by using SystemTray
class in java.awt
package I can do so but neither I'm getting any tutorial on it nor any working program example.
I'd asked this question here to either get the link to tutorial site for SystemTray
class or if any body knows how to trap the window minimizing event, a working example.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
WindowListener
接口和JFrame
的addWindowListener()
方法应该可以帮助您确定框架何时最小化。The
WindowListener
interface andJFrame
'saddWindowListener()
method should help you determine when the frame has been minimised.这将捕获窗口最小化事件并创建一个托盘图标。它还将从任务栏中删除该窗口,并在托盘图标上添加一个侦听器,以便单击鼠标即可恢复该窗口。该代码有点杂乱,但对于您的学习目的来说应该足够了:
This will trap the window minimized event and will create a tray icon. It will also remove the window from the taskbar and it will add a listener on the tray icon so that a mouseclick would restore the window. The code is a bit scrappy but should be good enough for your learning purposes:
最好的方法是创建如下
1) SystemTray
2) 添加
JPopopMenu
到SystemTray 的图标
3) 设置
DefaultCloseOperation
为 TopLevelContainer (在您的情况下 JFrame)通过使用 WindowListener
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
在其他情况下始终有效
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
注意不要忘记从
JMenuItem
或另一个Action/ 声明
,因为在这种形式下,当前的 JVM 永远不会离开本机操作系统,直到 PC 断电或重新启动System.exit(1)
到SystemTray 的 JpopupMenu
事件best way would be create follows
1) SystemTray
2) add
JPopopMenu
to theSystemTray's Icon
3) set
DefaultCloseOperation
for TopLevelContainer (in your case JFrame)by using WindowListener
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
in other cases always works
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
notice don't forget declare
System.exit(1)
to theSystemTray's JpopupMenu
, fromJMenuItem
or anotherAction/Event
, because in this form currenet JVM never gone from Native OS until PC power-off or restartWindowStateListener
文档Frame.getExtendedState()
文档WindowStateListener
docsFrame.getExtendedState()
docs