开发者

path of an image in an eclipse project

I'm trying to display two pictures on my JFrame, the way I found was to use icons and JLabels, this seems pretty straightforward and I'm not having problems with this. But when it comes to locating the image I can't get it to work. I'm on a lin开发者_如何学运维ux machine thus the forwardslash style. I created a folder called pics in my project which is called 399assig1.

        ImageIcon icon1 = createImageIcon("/home/dsk03/ugrad/jeanbern/workspace/C291/workspace/399assig1/pics/fur-05.jpg","First");
    this.label1 = new JLabel("Picture 1", icon1, JLabel.CENTER);

    ImageIcon icon2 = createImageIcon("pics/fur.png","Second");
    this.label2 = new JLabel("Picture 2", icon2, JLabel.CENTER);

this is the error I get

Couldn't find file: /home/dsk03/ugrad/jeanbern/workspace/C291/workspace/399assig1/pics/fur-05.jpg
Couldn't find file: pics/fur.png


if createImageIcon() is searching the CLASSPATH for the file, you'd need to add the root directory to the CLASSPATH. A better approach would be use a path that is relative to a directory that is already included in the CLASSPATH.

Like so:

%>CLASSPATH=$CLASSPATH;/home/dsk03/ugrad/jeanbern/workspace  

then your call would be

ImageIcon icon1 = createImageIcon("399assig1/pics/fur-05.jpg", "MyIcon");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜