What is the correct path to display an ImageIcon png file for Windows 7?

前端 未结 6 1665
南笙
南笙 2020-12-04 02:20

I wanted to test having a program with a simple png image on it. I wrote a short program that does this, but I can\'t seem to get the path right. I have checked, checked aga

6条回答
  •  难免孤独
    2020-12-04 02:59

    Firstly, you've supplied a relative path, so the system is looking for the image relative to the location you executed the program.

    Secondly, the path should have a drive spec or at least a leading /. Depending on your setup, something like 'C:/Users/Evan/javaItems/Sprites_and_Other_Art/green.png' should work (you may need to change the drive spec to meet your system)

    Thirdly, make sure that the file exists in the specified location, System.out.println(new File("C:/Users/Evan/javaItems/Sprites_and_Other_Art/green.png").exists()) should return true, other wise the file is in the wrong location.

    A relative path basically means a path location relative to the programs execution. So, if you were running the program from C:/Program Files/MyAwesomeApplication for example, a relative path of Users/Evan/javaItems/Sprites_and_Other_Art/green.png would become an absolute path of C:/Program Files/MyAwesomeApplication/Users/Evan/javaItems/Sprites_and_Other_Art/green.png. This describes the path from the root location to the file/folder in question.

    You can test this by using System.out.println(new File("C:/Users/Evan/javaItems/Sprites_and_Other_Art/green.png").getAbsolutePath(‌​)) which will give you the full path.

提交回复
热议问题