5

我正在尝试使用 Java 在 Ubuntu 18.04 上制作系统托盘应用程序。

这是我正在执行的代码:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class App {
    static{
        System.setProperty("java.awt.headless", "false");
    }
    public static void main(String[] args) {

//       if(!SystemTray.isSupported()){
//           System.out.println("System Tray is not supported.");
//           return;
//       }
       final PopupMenu popup = new PopupMenu();
       Image img = Toolkit.getDefaultToolkit().createImage("/path/img.png");
       final TrayIcon trayIcon = new TrayIcon(img);
       final SystemTray systemTray = SystemTray.getSystemTray();

       //create components of system tray
        trayIcon.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                System.out.println("In here!");
                trayIcon.displayMessage("Test","Some action happened",TrayIcon.MessageType.INFO);
            }
        });

        try{
            systemTray.add(trayIcon);
        }catch(AWTException e){
            System.out.println("TrayIcon could not be added.");
        }

    }


}

我注释掉了 isSupported() 方法测试片段,因为我不断收到“不支持系统托盘”。

我得到的例外是:

线程“main”中的异常 java.lang.UnsupportedOperationException:当前平台不支持系统托盘。在 java.awt.SystemTray.getSystemTray(SystemTray.java:186) 在 App.main(App.java:16)

知道我将如何支持它吗?另外,如果有人有 MacOS 设备,你可以试试,让我知道它是否有效?谢谢!

4

1 回答 1

8

Gnome 3.28(用于 Ubuntu 18.04)移除了系统托盘。有一个名为TopIcon Plus Gnome Shell Exetension 的软件,它返回系统托盘。我测试了代码,效果符合预期。一个图标被放置在全局栏中。

https://extensions.gnome.org/extension/1031/topicons/

于 2018-05-28T23:33:20.743 回答