简体   繁体   English

如何在系统托盘中显示图标文件?

[英]How to show icon file in system tray?

I am adding my application in system tray when I close it. 关闭应用程序时,我会将其添加到系统托盘中。 But it is not displaying the icon. 但是它没有显示图标。 When I try to show picture file then it works fine but when I try with an icon file it does not work. 当我尝试显示图片文件时,它可以正常工作,但是当我尝试使用图标文件时,它不起作用。 How can I display an icon instead picture? 如何显示图标而不是图片?

Image image = new ImageIcon("src/resources/busylogo.jpg").getImage();
final TrayIcon trayIcon = new TrayIcon(image);
try {
  SystemTray.getSystemTray().add(trayIcon);
} catch (AWTException e2) {
  e2.printStackTrace();
}

Better use Toolkit to load an icon. 最好使用Toolkit加载图标。 That's a low size file, and an asynchronous load will give you less problems. 这是一个小文件,异步加载将给您带来更少的问题。 Try this code, which is recommanded by Sun. 尝试使用此代码,Sun推荐使用此代码。

SystemTray tray = SystemTray.getSystemTray();
Image image = Toolkit.getDefaultToolkit().getImage("src/resources/busylogo.jpg");
final TrayIcon trayIcon = new TrayIcon(image);
try {
   tray.add(trayIcon);
} catch (AWTException e2) {
   e2.printStackTrace();
}

More informations here : http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/systemtray/ 此处提供更多信息: http : //java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/systemtray/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM