16

I have a Java project called MyProject. I have a few different packages (keeping names simple for the purpose of this question), as follows:

src/PackageA
src/PackageA/PackageAa
src/PackageA/PackageAa/PackageAaa
src/PackageB
src/PackageB/PackageBa
src/PackageB/PackageBa/PackageBaa

I have a class

src/PackageA/PackageAa/PackageAaa/MyJavaFile.java

And I have an image

src/PackageB/PackageBa/PackageBaa/MyImage.png

Inside of MyJavaFile.java, I would like to declare an Image oject of MyImage.png

Image img = new Image(....what goes here?...)

How can I do this?

4

5 に答える 5

21

You could either call Class.getResource and specify a path starting with /, or ClassLoader.getResource and not bother with the /:

URL resource = MyJavaFile.class
      .getResource("/PackageB/PackageBa/PackageBaa/MyImage.png");

or:

URL resource = MyJavaFile.class.getClassLoader()
      .getResource("PackageB/PackageBa/PackageBaa/MyImage.png");

Basically Class.getResource will allow you to specify a resource relative to the class, but I don't think it allows you to use ".." etc for directory navigation.

Of course, if you know of a class in the right package, you can just use:

URL resource = SomeClassInPackageBaa.class.getResource("MyImage.png");

(I'm assuming you can pass a URL to the Image constructor in question. There's also getResourceAsStream on both Class and ClassLoader.)

于 2012-08-28T06:13:27.760 に答える
4

you can use relative path since the the relative path is project folder.

 ImageIcon img = new ImageIcon("src/PackageB/PackageBa/PackageBaa/MyImage.png");
于 2015-10-01T23:24:29.850 に答える
3
/folderB/folderBa/folderBaa/MyImage.png

The image can stored into a project folder location .eg: /images/MyImage.png

Then try:

Image img = new Image(/images/MyImage.png);

Using a file path is not possible when running a program that's in a jar file, especially if the program is being loaded as an applet or WebStart application then you can use ClassLoader to get image.

use the following code to load the images:

ClassLoader cldr = this.getClass().getClassLoader();

java.net.URL imageURL = cldr.getResource("/PackageB/PackageBa/PackageBaa/MyImage.png");
ImageIcon aceOfDiamonds = new ImageIcon(imageURL);
于 2012-08-28T06:12:34.913 に答える
1

This IS the best way to handle all images and icons in a JAR App.

Once you've zipped up all of your images and icons into its own JAR file - Configure your build path by adding the images JAR file into your libraries tab so that its now included in your classpath.

Then simply use the following 3x lines of code at the start of your constuctor to access any image you need for anything including a SystemTray image which doesn't accept the simple ImageIcon's as its main icon (weird I know). The 3x lines are:

  URL iconUrl = this.getClass().getResource("/image-iconb.png");
  Toolkit tk = this.getToolkit();
  imageIcon = tk.getImage(iconUrl);

(imageIcon is just a constructor declared Image variable) Now you can set a window icon as simply as:

setIconImage(imageIcon );

and at the same time use the same variable when setting the System TrayIcon by declaring:

 trayIcon = new TrayIcon(imageIcon, "SystemTray Demo", popupMenu);

The above allows you to declare Images or ImageIcons easily and centrally without running the risk of not keeping image resources in the right place. It keeps it nice and tidy, with the JAR containing all your images automatically compiled at run time and distribution of your program.

As a bonus, once the JAR is registered in your classpath - you can keep adding any other images into the same JAR at any time without any fuss too - Everything just works and the added images are instantly available to your app.

Much better in my view.

于 2014-12-30T22:57:21.507 に答える
0

Use the getResource method to read resources inside the src root. For example, the following code retrieves images from a folder src/images.

// Get current classloader
ClassLoader cl = this.getClass().getClassLoader();
// Create icons
Icon saveIcon  = new ImageIcon(cl.getResource("images/save.gif"));
Icon cutIcon   = new ImageIcon(cl.getResource("images/cut.gif"));

The example assumes that the following entries exist in the application's JAR file:

images/save.gif
images/cut.gif
于 2013-09-09T10:07:31.843 に答える