以编程方式关闭 Java 托盘气球
我正在使用 java.awt.SystemTray 创建和管理托盘图标和气球消息。一切正常。
但我想知道是否可以在显示消息后关闭或淡出该消息。
现在,用户需要单击气球或将其关闭,否则消息将不会消失。
I am using java.awt.SystemTray
to create and manage the tray icon and balloon messages. Everything works fine.
But I would like to know whether it is possible to close or fade the message after it is displayed.
Right now, the user needs to click the balloon or close it, otherwise the message will not go away.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定其他平台,但在 Windows 上,只有当机器正在使用时,即用户正在输入内容或移动鼠标时,该消息才会消失。如果您不移动鼠标或键入任何内容,消息将保留在原来的位置。
下面的代码向我显示了一条消息。如果我移动鼠标,它会在大约 5 秒后消失。如果我不这样做,它会一直存在,直到我移动鼠标或键入某些内容,此时它就会消失。
没有直接的方法可以在时间结束之前删除消息 - 但是,您可以删除
TrayIcon
(如果需要,可以立即重新添加它,尽管确实不建议这样做)。删除TrayIcon
会导致消息也被删除。添加到上面的以下代码会导致
TrayIcon
和消息都被删除:注意:
AWTException
TrayIcon
可能是一个坏主意。由于如果用户正在使用机器,消息确实会消失,因此最好不要这样做。I'm not sure about other platforms, but on Windows, the message will only disappear if the machine is in use - i.e. if the user is typing something or moving the mouse. If you don't move the mouse or type anything, the message will stay where it is.
The following code shows a message for me. If I move the mouse, it disappears about 5 seconds later. If I don't, it stays around until such time as I move the mouse or type something, at which point it disappears.
There's no direct way to remove the message before its time is up - however, you can remove the
TrayIcon
(and if necessary immediately re-add it, although this really isn't recommended). Removing theTrayIcon
causes the message to also be removed.The following code, added to the above, causes the
TrayIcon
and the message to both be removed:Notes:
AWTException
Thread.sleep
in the second block is strictly for demonstration purposes. Since this code will execute on the AWT thread, you should not include that sleep in actual code.TrayIcon
in this way is probably a bad idea. Since the message does disappear if the user is using the machine, it's probably wisest not to do this.