添加 ImageIcon 的路径 - Java

发布于 2024-11-07 11:12:57 字数 360 浏览 12 评论 0原文

嘿,所以我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

念三年u 2024-11-14 11:12:57

关于如何使用图标的 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 the createImageIcon() method and the description that follows it.

ㄖ落Θ余辉 2024-11-14 11:12:57

您可以将图像放入当前文件夹或子文件夹中,然后使用以下路径(例如):

System.getProperty("user.dir") + "images\pic1.gif"

You can put your images in the current folder or subfolders and then use the following path (for example):

System.getProperty("user.dir") + "images\pic1.gif"
紫竹語嫣☆ 2024-11-14 11:12:57

只需不要使用绝对路径,并将图像放入源或二进制文件所在的子目录中。

Simply do not use absolute path, and put the images into a subdirectory where is located your sources or binaries.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文