gpt4 book ai didi

java - 在java中更改系统托盘图标

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:48:40 25 4
gpt4 key购买 nike

我只想为我的应用程序更改系统托盘图标图像。我做了两件事 -

只是更改了默认程序中的 URL -

final TrayIcon trayIcon = new TrayIcon(createImage("images/Graph.png", "tray icon"));

第二次尝试-

Image img = Toolkit.getDefaultToolkit().getImage("images/Graph.png");
final TrayIcon trayIcon = new TrayIcon(img, "Application Name", popup);

应用程序在这两种情况下都会启动,但不会显示任何图像。它是一个空白占位符。我做错了什么?

最佳答案

images/Graph.png 不是位于您的 jar 中的图像的有效 URL。因此,我猜 img 在您第二次尝试时为 null。

我建议你这样:

//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);

您还应确保 images/ 在您的类路径中。

关于java - 在java中更改系统托盘图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17745140/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com