如何动态启用或禁用 Java Swing 程序的任务栏图标
我正在使用 JFrame 编写一个基于 Java Swing 的程序,该程序能够显示系统托盘图标以快速访问最常用的功能。现在我想添加一个选项,供用户选择在程序窗口最小化时是否显示普通(Windows)任务栏图标。
在 Google 中搜索告诉我可以使用 JDialog 而不是 JFrame。不幸的是,这对我来说不是一个好的解决方案,因为我想根据用户的决定动态启用或禁用任务栏图标。
这有可能吗?
谢谢并亲切的问候,马蒂亚斯
I am writing a Java Swing-based program using and JFrame that is able to display a system tray icon for quick access to most-used features. Now I want to add an option for the user to choose whether or not the normal (Windows) taskbar icon should be displayed when the program window is minimized.
A search in Google told me that I can use JDialog instead of JFrame. Unfortunately that is not a good solution in my case, because I want to dynamically enable or disable the task bar icon based on the user's decision.
Is that possible somehow?
Thanks and kind regards, Matthias
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
JDialog 或 JFrame 只是一个容器。在它们之间切换是否不适合您的情况?当您需要切换时,只需创建一个设置为相同位置和大小的其他类型的新实例,然后将 contentPane 移过去即可。
A JDialog or JFrame is just a container. Would switching between them not work for your situation? When you need to switch, just create a new instance of the other type set to the same location and size, and move the contentPane over.
值得尝试的事情,虽然我不完全确定这会起作用,因为现在已经很晚了,我可能以错误的方式思考这个问题。
当您最小化窗口时,会触发一个事件,您要做的就是通过将 WindowStateListener 添加到监视 WINDOW_ICONIFIED 和 WINDOW_DEICONIFIED 的 JFrame 来捕获该事件。当WINDOW_ICONIFIED发生时,将JFrame的visible属性设置为false;当 WINDOW_DEICONIFIED 将其设置为 true 时。将框架可见性设置为 false 的快速测试似乎将其从任务栏中删除,您所要做的就是弄清楚它是否确实有效,然后实现状态列表器。
这是我用来测试的代码
Something to try, though I am not entirely sure this will work becase it is late here and I may be thinking about this the wrong way.
When you minimize a window, an event is triggered, what you want to do is catch it by adding a WindowStateListener to the JFrame that watches for WINDOW_ICONIFIED and WINDOW_DEICONIFIED. When WINDOW_ICONIFIED occurs, set the visible property of the JFrame to false; when WINDOW_DEICONIFIED set it to true. A quick test of setting a frames visibility to false seemed to remove it from the task bar, all you have to do is figure out if it does actually work and then implement a state listner.
Here is the code I used to test