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");
精彩评论