简体   繁体   中英

Where should I store images (and other files) to be used by my program in Java?

I'm trying to create a (very) simple GUI program which allows the user to select from different radio buttons to display an image in a JLabel. The code I'm using is this:

 JLabel imageLabel = new JLabel();
 imageLabel.setIcon(new ImageIcon("images/Dog"));

My question is, where do I put this "images" folder? When I put it in the src and the bin folder, the images still didn't load. I ended up trying the following code which worked when I used the bin folder:

 imageLabel.setIcon(new ImageIcon(getClass().getResource("images/Dog")));

The problem is, from what I understand the bin folder isn't supposed to be used for these files? Also, I'm pretty sure the images folder I put in there got deleted (which would be a pretty good reason not to use it...).

I'm sending the directory to my professor, so I don't want the file path to work exclusively on my computer.

PS I'm using Eclipse, if that matters.

Just put it in the project's folder. Example:

(示例图片)

You would put your images folder where my " del_icon " image is. Just right in the folder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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