添加 ImageIcon 的路径 - Java
嘿,所以我在java方面非常糟糕,我想添加一个ImageIcon,现在我已经完成了这个,我什至有图像可以工作,唯一的问题是我正在制作的程序是为了大学工作,当我提交工作将通过 .rar 文件在线提交。
所以我的问题是目前图像有一个巨大的直接路径,即
C:\Users\MY-NAME\Documents\NetBeansProjects\UNI-PROJECT\src\IMAGES\image.png
因为他们将审查我在他们的工作电脑或家用电脑上工作,无论图像对他们不起作用,因为它使用的是我的家庭参考。我如何引用图像,以便他们可以在任何地方打开它并且图像仍然有效?
提前致谢
-SKENG-
Hey So I'm pretty much awful at java and I want to add a ImageIcon now I've done this before and I even have the images to work the only issue is the program I am making is for University work and when I submit the work it will be submitted online through a .rar file.
So my issue is currently the image's have a huge direct path so i.e.
C:\Users\MY-NAME\Documents\NetBeansProjects\UNI-PROJECT\src\IMAGES\image.png
Since they will be reviewing my work on their work PC or home PC whatever the images wont work for them since it's using my home reference. How can I reference a image so they can open it where ever and the images will still work?
Thanks in advance
-SKENG-
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
关于如何使用图标的 Java 教程 有一小部分解释如何使用
Class#getResource()
当图像包含在应用程序中时 - 查看createImageIcon()
方法和后面的说明它。The Java Tutorial on How to Use Icons has a small section explaining how to use
Class#getResource()
when the image is included with the application - look at thecreateImageIcon()
method and the description that follows it.您可以将图像放入当前文件夹或子文件夹中,然后使用以下路径(例如):
You can put your images in the current folder or subfolders and then use the following path (for example):
只需不要使用绝对路径,并将图像放入源或二进制文件所在的子目录中。
Simply do not use absolute path, and put the images into a subdirectory where is located your sources or binaries.