我正在尝试使用 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 设备,你可以试试,让我知道它是否有效?谢谢!