putting picture in java program using MyEclipse GUI
I want to add a picture to my GUI program created using Eclipse and MyEclipse (for GUI visual design) from the resource pictures I pasted earlier in开发者_开发百科 the project.
I managed to load pictures that lies just beside the .JAR file using
image = ImageIO.read(new File("imageFile.jpg"));
But I want to use the image from my resources "src" folder directly , so that the .JAR file is a standalone file yet loads pictures nicely.
I tried to make it
image = ImageIO.read(new File("src/ldtlogo3.jpg"));
I use this method when exporting the .JAR file Java: export to an .jar file in eclipse
Use the overloaded ImageIO.read
method taking an InputStream
as a parameter, and use MyClass.class.getResourceAsStream()
to get this input stream. getResourceAsStream
loads a resource from the classpath (and thus from the JAR of your application). Its api doc will tell you which path it expects.
Note that the src directory is used to hold your Java source files. The jar doesn't contain it. It contains the .class files, in a hierarchy which directly maps the package hierarchy. Eclipse will automatically "compile" the image file by copying to the output directory, along with the .class files.
精彩评论