java - 如何将 Java 应用程序放入系统托盘?

标签 java system-tray minimize

我有一个小控制面板,只是我制作的一个小应用程序。我想用系统最小化/上下控制面板,以及电池生命周期、日期、网络等。

谁能给我线索、教程链接或阅读内容?

最佳答案

从 Java 6 开始,SystemTray 支持此功能。和 TrayIcon类。 SystemTray 在其 Javadocs 中有一个相当广泛的示例:

TrayIcon trayIcon = null;
if (SystemTray.isSupported()) {
    // get the SystemTray instance
    SystemTray tray = SystemTray.getSystemTray();
    // load an image
    Image image = Toolkit.getDefaultToolkit().getImage("your_image/path_here.gif");
    // create a action listener to listen for default action executed on the tray icon
    ActionListener listener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            // execute default action of the application
            // ...
        }
    };
    // create a popup menu
    PopupMenu popup = new PopupMenu();
    // create menu item for the default action
    MenuItem defaultItem = new MenuItem(...);
    defaultItem.addActionListener(listener);
    popup.add(defaultItem);
    /// ... add other items
    // construct a TrayIcon
    trayIcon = new TrayIcon(image, "Tray Demo", popup);
    // set the TrayIcon properties
    trayIcon.addActionListener(listener);
    // ...
    // add the tray image
    try {
        tray.add(trayIcon);
    } catch (AWTException e) {
        System.err.println(e);
    }
    // ...
} else {
    // disable tray option in your application or
    // perform other actions
    ...
}
// ...
// some time later
// the application state has changed - update the image
if (trayIcon != null) {
    trayIcon.setImage(updatedImage);
}
// ...

您也可以查看 this article , 或 this tech tip .

关于java - 如何将 Java 应用程序放入系统托盘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/758083/

相关文章:

java - 短语匹配引擎

java - Spring 启动: Cannot access REST Controller on localhost (404)

java - 在开始另一个任务之前等待一个任务在 Java 中完成

c++ - 一种确保系统托盘图标被删除的方法......保证

python - 如何为应用程序制作自定义托盘图标?

python - 如何使用 Scipy 最小化函数的一个参数?

jquery-ui - 为什么 jQuery UI 对话框没有最小化、最大化按钮?

java - 来自包含文件(文件 :///path/to/file) to a java. nio.FileChannel

c# - 单击一次部署后的通知图标设置

python - scipy 最小化函数的输入结构