如何动态启用或禁用 Java Swing 程序的任务栏图标

发布于 2024-10-03 02:47:29 字数 243 浏览 8 评论 0原文

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

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

发布评论

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

评论(2

暖心男生 2024-10-10 02:47:29

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.

卸妝后依然美 2024-10-10 02:47:29

值得尝试的事情,虽然我不完全确定这会起作用,因为现在已经很晚了,我可能以错误的方式思考这个问题。

当您最小化窗口时,会触发一个事件,您要做的就是通过将 WindowStateListener 添加到监视 WINDOW_ICONIFIED 和 WINDOW_DEICONIFIED 的 JFrame 来捕获该事件。当WINDOW_ICONIFIED发生时,将JFrame的visible属性设置为false;当 WINDOW_DEICONIFIED 将其设置为 true 时。将框架可见性设置为 false 的快速测试似乎将其从任务栏中删除,您所要做的就是弄清楚它是否确实有效,然后实现状态列表器。

这是我用来测试的代码

import java.awt.*;

public class FrameTest
{
    public static void main (String args[]) throws Exception
    {
        // Create a test frame
        Frame frame = new Frame("Hello");
        frame.add ( new Label("Minimize demo") );
        frame.pack();

        // Show the frame
        frame.setVisible (true);

        // Sleep for 5 seconds, then minimize
        Thread.sleep (5000);
        frame.setState ( Frame.ICONIFIED );
        frame.setVisible(false);
        // Sleep for 5 seconds, then restore
        Thread.sleep (5000);
        frame.setState ( Frame.NORMAL );
        frame.setVisible(true);

        // Sleep for 5 seconds, then kill window
        Thread.sleep (5000);
        frame.setVisible (false);
        frame.dispose();

        // Terminate test
        System.exit(0);
    }
}

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

import java.awt.*;

public class FrameTest
{
    public static void main (String args[]) throws Exception
    {
        // Create a test frame
        Frame frame = new Frame("Hello");
        frame.add ( new Label("Minimize demo") );
        frame.pack();

        // Show the frame
        frame.setVisible (true);

        // Sleep for 5 seconds, then minimize
        Thread.sleep (5000);
        frame.setState ( Frame.ICONIFIED );
        frame.setVisible(false);
        // Sleep for 5 seconds, then restore
        Thread.sleep (5000);
        frame.setState ( Frame.NORMAL );
        frame.setVisible(true);

        // Sleep for 5 seconds, then kill window
        Thread.sleep (5000);
        frame.setVisible (false);
        frame.dispose();

        // Terminate test
        System.exit(0);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文