简体   繁体   English

为ImageIcon添加路径-Java

[英]Adding a path for a ImageIcon - Java

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. 嘿,所以我在Java方面非常糟糕,现在我想添加一个ImageIcon,我什至可以使图像工作,唯一的问题是我正在编写的程序是用于大学工作的,当我提交该程序时工作将通过.rar文件在线提交。

So my issue is currently the image's have a huge direct path so ie 所以我的问题是图像当前有一条巨大的直接路径,即

C:\\Users\\MY-NAME\\Documents\\NetBeansProjects\\UNI-PROJECT\\src\\IMAGES\\image.png 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. 由于他们会在我的工作PC或家用PC上审查我的工作,因此图像对他们来说都不起作用,因为它们使用的是我的家庭参考。 How can I reference a image so they can open it where ever and the images will still work? 如何引用图像,以便他们可以在任何地方打开它,并且图像仍可以工作?

Thanks in advance 提前致谢

-SKENG- -SKENG-

Java教程“如何使用图标”中有一小节说明了当图像包含在应用程序中时如何使用Class#getResource() -查看createImageIcon()方法及其createImageIcon()的描述。

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

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

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

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

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