我有一个运行java web start(jnlp文件)的javafx项目 应用程序启动时,它会在桌面上创建一个快捷方式并自动启动。在启动期间,它会在系统托盘中创建一个图标。创建此系统托盘图标时,出现错误。
创建系统托盘图标的代码是:
private TrayIcon trayIcon;
private PopupMenu popupMenu;
public void createTrayIcon() {
if (SystemTray.isSupported()) {
SystemTray tray = SystemTray.getSystemTray();
String imageString = "image.jpg";
Image image = Toolkit.getDefaultToolkit().getImage(TestSystemTray.class.getResource(imageString));
System.out.println("image is: " + image);
popupMenu = getPopupMenu();// gets the popup with menus
trayIcon = new TrayIcon(image, "Test application", popupMenu);
try {
if (tray != null) {
System.out.println("Tray is no null: " + tray);
if (trayIcon != null) {
System.out.println("trayIcon is no null:" + trayIcon);
trayIcon.setImageAutoSize(true);
setEventHandler();
tray.add(trayIcon);
showProgramIsMinimizedMsg();
} else {
System.out.println("trayIcon is null");
}
} else {
System.out.println("tray is null");
}
} catch (AWTException e) {
System.out.println("Can't add tray to systemtray" + e.getMessage() + e);
}
}
}
我认为这是因为在调用“tray.add(trayIcon);”时tray calles getTrayIcons();在我的情况下给出一个nullpointer和java.awt.SystemTray.getTrayIcons(未知来源)。为了确认这一点,我添加了“TrayIcon [] icons = tray.getTrayIcons();”和“System.out.println(”getTrayIcons()是:“+ icons);”在调用“tray.add(trayIcon)”之前,它已被确认。
这个奇怪的是,如果我关闭应用程序并通过桌面快捷方式重新打开它,则不再出现错误。
仅供参考:JNLP文件包含所有权限,并且JAR文件已签名并获得批准。 Java 1.8.0_05
任何?