简体   繁体   English

获取图像路径以将其用作系统任务栏图标

[英]Getting image path to use it as system tray icon

So I've been looking at many, many solutions to this "problem" but somehow nothing worked for me. 因此,我一直在研究针对此“问题”的许多解决方案,但无论如何对我无济于事。 I'm trying to set a system tray icon for my Java application, but no matter what I do, the icon just won't show up. 我正在尝试为Java应用程序设置系统任务栏图标,但是无论我做什么,该图标都不会显示。 Instead there's a blank spaceholder. 而是有一个空白的空格。 I've tried different things to get the image path but it seems like I can't get the correct image path. 我尝试了其他方法来获取图像路径,但似乎无法获取正确的图像路径。

This is what the code currently looks like: 这是当前代码的样子:

URL url = System.class.getResource("image.jpg");
Image img = Toolkit.getDefaultToolkit().getImage(url);
SystemTray mainTray = SystemTray.getSystemTray();
TrayIcon trayIconImage = new TrayIcon(img, "tray icon");
mainTray.add(trayIconImage);

System.print.out(url) returns null System.print.out(url)返回null

What am I doing wrong when getting the path? 上路时我在做什么错? I can post a screenshot of the Eclipse folders in case that helps. 我可以发布Eclipse文件夹的屏幕截图,以防万一。

You can drag and drop the image into the package folder where your .java files are shown inside the IDE. 您可以将图像拖放到package文件夹中,您的.java文件将显示在IDE中。 Just drag the file from the desktop, or whatever location, and drop it into the IDE where you see your source files. 只需将文件从桌面或任何位置拖放到IDE中即可看到源文件。 after that you can simply refer to the image, "image.jpg", in your code when declaring a File or Image, or whatever class you use to make it into an object. 之后,在声明文件或图像时,您可以在代码中简单地引用图像“ image.jpg”,或使用任何使它成为对象的类。 When you export the project the image will be included so you will not have to worry about the path. 导出项目时,将包含图像,因此您不必担心路径。 Should be similar to what I did in the photo. 应该类似于我在照片中所做的。 I dragged and dropped the int.txt file into the source package. 我将int.txt文件拖放到了源包中。 将int.txt文件添加到程序路径

if this was helpful accept the answer. 如果这有帮助,请接受答案。

You should be able to use this: 您应该可以使用:

Image img = new BufferedImage("image.jpg");
SystemTray mainTray = SystemTray.getSystemTray();
TrayIcon trayIconImage = new TrayIcon(img, "tray icon");
mainTray.add(trayIconImage);

Basically the program can find the file because they are in the same directory which is the source packages folder. 基本上,程序可以找到该文件,因为它们位于源包文件夹所在的目录中。

You may want to try using the Path class and File class if you are worried about getting paths. 如果您担心获取路径,则可能要尝试使用Path类和File类。

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

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