我是一个有用的助手,可以为您翻译文本。
我正在尝试使用Java制作Ubuntu 18.04上的系统托盘应用程序。
这是我执行的代码:
我正在尝试使用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()方法测试片段,因为我一直收到“系统托盘不受支持”的消息。
我得到的异常是:
Exception in thread "main" java.lang.UnsupportedOperationException: 在当前平台上不支持系统托盘。 at java.awt.SystemTray.getSystemTray(SystemTray.java:186) at App.main(App.java:16)
你有什么想法可以让它被支持吗? 此外,如果有人有MacOS设备,请尝试一下,并让我知道是否有效?谢谢!