how to add image from spectified package into label, frames etc
I have images i want to add in the following folder: G:\开发者_Go百科my java\DesktopApplication1\build\classes\desktopapplication1\resources.
How to go about adding image in this folder to my labels or frames?
Once built, the image will typically be inside a Jar. The resources in a Jar can be accessed a number of ways, here is one.
URL urlToImage = this.getClass().getResource(
"/desktopapplication1/resources/the.png");
ImageIcon imageIcon = new ImageIcon(urlToImage);
JLabel ...
You can do something like this:
ImageIcon image = new ImageIcon("path_to_image");
JLabel label = new JLabel(image);
I did something like this:
JLabel l2=new JLabel("");
try {
Image img = ImageIO.read(getClass().getResource("resources/wait20.gif"));
l2.setIcon(new ImageIcon(img));
}
catch (IOException ex) {
}
It does work, but i would have liked it more had the GIF animation was displayed. Nevertheless, if a static image is to be displayed, this is the way to do it.
精彩评论