以编程方式关闭 Java 托盘气球

发布于 2024-12-06 15:50:30 字数 122 浏览 7 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

同展鸳鸯锦 2024-12-13 15:50:30

我不确定其他平台,但在 Windows 上,只有当机器正在使用时,即用户正在输入内容或移动鼠标时,该消息才会消失。如果您不移动鼠标或键入任何内容,消息将保留在原来的位置。

下面的代码向我显示了一条消息。如果我移动鼠标,它会在大约 5 秒后消失。如果我不这样做,它会一直存在,直到我移动鼠标或键入某些内容,此时它就会消失。

final TrayIcon ti = new TrayIcon(XTPSkin.getInstance().getAppIcon().getImage());
final SystemTray st = SystemTray.getSystemTray();
st.add(ti);
ti.displayMessage("foo", "bar", MessageType.INFO);

没有直接的方法可以在时间结束之前删除消息 - 但是,您可以删除 TrayIcon (如果需要,可以立即重新添加它,尽管确实不建议这样做)。删除 TrayIcon 会导致消息也被删除。

添加到上面的以下代码会导致 TrayIcon 和消息都被删除:

SwingUtilities.invokeLater(new Runnable(){
    public void run() {
        try {
            Thread.sleep(1000); // Don't do this
            st.remove(ti);
        } catch (InterruptedException ie) {
            // You won't need this when the sleep is removed
        }
    }
});

注意:

  • 您需要使用 AWTException
  • 第二个块中的 Thread.sleep 严格用于演示目的。由于此代码将在 AWT 线程上执行,因此您不应在实际代码中包含该睡眠。
  • 以这种方式不断删除并重新添加 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.

final TrayIcon ti = new TrayIcon(XTPSkin.getInstance().getAppIcon().getImage());
final SystemTray st = SystemTray.getSystemTray();
st.add(ti);
ti.displayMessage("foo", "bar", MessageType.INFO);

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 the TrayIcon causes the message to also be removed.

The following code, added to the above, causes the TrayIcon and the message to both be removed:

SwingUtilities.invokeLater(new Runnable(){
    public void run() {
        try {
            Thread.sleep(1000); // Don't do this
            st.remove(ti);
        } catch (InterruptedException ie) {
            // You won't need this when the sleep is removed
        }
    }
});

Notes:

  • You need to wrap the first block of code above with some exception handling for AWTException
  • The 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.
  • Continually removing and re-adding the 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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文