我有一個程序,當程序在系統托盤中顯示圖標時。我使用下面的代碼在系統托盤區顯示圖標:系統托盤圖像未加載java
public static void showTrayIcon() {
if (java.awt.SystemTray.isSupported()) {
st = java.awt.SystemTray.getSystemTray();
image = Toolkit.getDefaultToolkit().getImage(PongeeUtil.class.getClass().getResource("export.png"));
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Hello");
}
};
PopupMenu popup = new PopupMenu();
MenuItem defaultItem = new MenuItem("sdf");
defaultItem.addActionListener(listener);
popup.add(defaultItem);
trayIcon = new TrayIcon(image, "Tray Demo", popup);
trayIcon.addActionListener(listener);
try {
st.add(trayIcon);
} catch (AWTException e) {
System.err.println(e);
}
}
}
當我打電話,我main()
這個方法我在系統托盤中得到的東西,但圖標丟失。我認爲圖像無法加載。圖像位於我的java文件所在的相同包中。
我在這裏做錯了什麼?
哪裏是'export.png'位於範圍內來源?考慮使用'ImageIO.read'來加載圖像,當它無法加載時會引發錯誤。請參閱[閱讀/加載圖像](http://docs.oracle.com/javase/tutorial/2d/images/loadimage.html)以獲取更多詳細信息 – MadProgrammer 2015-03-13 07:21:17
圖像與我的java文件位於同一個包中。 – 2015-03-13 07:24:39