简体   繁体   中英

how to import icons from other packages in java

Ok so I use use this code to import an icon from the same file that the class is in and assign it a JLabel ,

JLabel label1;
Icon iron_ore = new ImageIcon(getClass().getResource("icon_ore"));
label1 = new JLabel(iron_ore);

But what code do I have to add if I want to put the icon in a file different from witch the class is in? Thank you.

If the image is in the package foo.bar.baz , you use

getClass().getResource("/foo/bar/baz/the-icon.png")

as explained in the javadoc, of course.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM