开发者

Eclipse project with ImageIcon

开发者 https://www.devze.com 2023-01-19 11:08 出处:网络
I have an eclipse project with image folder. I need to create an ImageIcon from an image located in this folder but can not figure out how to tell it to look in the folder with out giving exact positi

I have an eclipse project with image folder. I need to create an ImageIcon from an image located in this folder but can not figure out how to tell it to look in the folder with out giving exact position on my system which I do not want to do as t开发者_如何学运维he project gets moved around to different pc's.

Code example

ImageIcon ai = new ImageIcon("/somedir");

Thanks for the help.


you have to store the image folder into your projectand then:

java.net.URL imageURL = this.getClass().getClassLoader().getResource("path_to_image_folder");
ImageIcon aceOfDiamonds = new ImageIcon(imageURL);

The path to the image folder can be relative to your class or absolute (starts with "/").

You can take a look here: How to use icons


It's very easy (remove the backslash):

ImageIcon ai = new ImageIcon("somedir");
0

精彩评论

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

关注公众号