简体   繁体   中英

Image Path in Eclipse using Windows 7 vs Mac OS X

I am sure this has a simple fix but I'm having a hard time finding a solution.

I normally use my Macbook pro, but I am trying to make sure that a project I'm working on is functioning in Windows 7 as well. I'm running bootcamp on my Macbook pro right now.

My only issue is importing images.

Using Eclipse on my Mac, I can just import the images into my project folder, look at their properties, and C+P the direct path into my import statement. However, this method is not working when I switch over to Windows 7. Since Windows 7 uses back slashes, I tried putting the entire path using \\ to replace each single \\ in the path. This does not work. I've also tried simply doing "\\Project\\image.png" but that also does not work. With single or double back slashes.

Here is my code for importing my file:

picLabel.setIcon(new ImageIcon("C:\\Users\\Kaitlyn\\Documents\\JAVA\\Project\\Project1\\src\\Project\\icon.png"));

I have yet to understand why it's not importing the images. The code has not been modified, except for the paths, from the code I used on Mac OS X. Is there something I am doing incorrectly when trying to import images using Windows 7?

Thank you!

You should just be able to do something like the following:

picLabel.setIcon(new ImageIcon("/Project/icon.png"));

Using the file path like in this example is a better way to retrieve files in any program. This makes it more compatible. So if you were going to deploy this program to other servers/machines you would be looking for the file in your project and not on the machine.

Here are some other questions and docs that might help. https://docs.oracle.com/javase/tutorial/uiswing/components/icon.html

How to add an ImageIcon to a JFrame?

Java Swing ImageIcon, where to put images?

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