[英]Change System tray Icon in java
I just want to change the system tray Icon image for my application. 我只是想为我的应用程序更改系统托盘图标图像。 I did 2 things -
我做了两件事 -
Just changed the URL in the default program - 刚刚更改了默认程序中的URL -
final TrayIcon trayIcon = new TrayIcon(createImage("images/Graph.png", "tray icon"));
Second try - 第二次尝试 -
Image img = Toolkit.getDefaultToolkit().getImage("images/Graph.png");
final TrayIcon trayIcon = new TrayIcon(img, "Application Name", popup);
The application launches in both cases but no image is shown. 应用程序在两种情况下都会启动,但不会显示图像。 Its a blank placeholder.
它是一个空白的占位符。 What am i doing wrong ?
我究竟做错了什么 ?
images/Graph.png
is not a valid URL for an image located in your jar. images/Graph.png
不是jar中图像的有效URL。 Hence, I guess that img
is null on your second try. 因此,我猜你的第二次尝试
img
是null。
I suggest you this way : 我建议你这样:
//Get the URL with method class.getResource("/path/to/image.png")
URL url = System.class.getResource("/images/Graph.png");
//Use it to get the image
Image img = Toolkit.getDefaultToolkit().getImage(url);
final TrayIcon trayIcon = new TrayIcon(img, "Application Name", popup);
You shall also ensure that images/
is in your classpath. 您还应确保
images/
在您的类路径中。
The problem is the way you include the image file as the image is inside your . jar
问题是您包含图像文件的方式,因为图像在您的内部
. jar
. jar
, use getResource()
or getResourceAsStream
, try this: . jar
,使用getResource()
或getResourceAsStream
,试试这个:
try {
InputStream inputStream= ClassLoader.getSystemClassLoader().getResourceAsStream("/images/Graph.png");
//or getResourceAsStream("/images/Graph.png"); also returns inputstream
BufferedImage img = ImageIO.read(inputStream);
final TrayIcon trayIcon = new TrayIcon(img, "Application Name", popup);
}
catch (IOException e) {}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.