image not loading

2019-08-03 12:05发布

问题:

trying to run the code

// Create a label with an image
Image image = new Image(display, "interspatial.gif");
Label imageLabel = new Label(shell, SWT.NONE);
imageLabel.setImage(image);

is giving me the error message

Exception in thread "main" org.eclipse.swt.SWTException: 
        i/o error (java.io.FileNotFoundException: interspatial.gif 
        (O sistema não pode encontrar o arquivo especificado))
    at org.eclipse.swt.SWT.error(Unknown Source)
    at org.eclipse.swt.SWT.error(Unknown Source)
    at org.eclipse.swt.graphics.ImageLoader.load(Unknown Source)
    at org.eclipse.swt.graphics.ImageDataLoader.load(Unknown Source)
    at org.eclipse.swt.graphics.ImageData.<init>(Unknown Source)
    at org.eclipse.swt.graphics.Image.<init>(Unknown Source)
    at examples.ch5.LabelExample.main(LabelExample.java:31)
Caused by: java.io.FileNotFoundException:
        interspatial.gif 
        (O sistema não pode encontrar o arquivo especificado)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:106)
    at java.io.FileInputStream.<init>(FileInputStream.java:66)
    at org.eclipse.swt.internal.Compatibility.newFileInputStream(Unknown Source)
    ... 5 more

Additional information:

In Eclipse, I had expanded Chapter05, then examples.ch5, then right-clicked LabelExample.java, then chose Run As, then 1 Java Application.

I tried placing interspatial.gif in the Chapter05 dir, the examples dir, the ch5 dir and the images dir (probably related to an other source code from the same chapter).

There is "a package examples.ch5;" line in the beginning of the file.

Why is the image not loading?

回答1:

It is throwing java.io.FileNotFoundException. So ither you gave a wrong path or your image is not available at the given path.

So just go to your Eclipse workspace and open that project which you are trying to run (Chapter05) and paste your interspatial.gif image with file LabelExample.java in src folder. Then I think it should work.



回答2:

It looks like your image is in the wrong place or has the wrong name. Make sure the name is identical in your code, and copy it into a few new places to see if you can get it to display.

The image probably needs to go in the exact same folder as the source file, probably 'src'.



回答3:

The second parameter of ...

new Image(display, "interspatial.gif")

... is a file path. Since "interspatial.gif" is not an absolute path, but a relative path, it relates to the current working directory (CWD) where your application is executed.

When you run your app within Eclipse, then the default CWD is the project root. Thus moving the "interspatial.gif" file to the root of your Eclipse project will solve the problem.



回答4:

In your example,

Image image = new Image(display, "interspatial.gif");

your image needs to exist in the same path as this code. If this is not what you want, you can use your plugin's Activator#getImageDescriptor(String path), which uses the plugin relative path.

As an aside, if you want to reuse this image, you can override AbstractUIPlugin#initializeImageRegistry(ImageRegistry reg) and add the image to the registry, later getting it via AbstractUIPlugin#getImageRegistry()#get(String key). The registry deals with the disposal of the image.



标签: java swt jface