I am trying to display an icon in a JButton, but when I run the application, it just shows a blank JButton with no image inside. The path is correct and I am not getting any errors...just a blank button. I'm not sure where I am going wrong.
JButton quickGuide = new JButton();
Icon icon1 = new ImageIcon("/Project1/images/quickGuideIcon.png");
quickGuide.setIcon(icon1);
quickGuide.setBounds(490, 10, 50, 50);
quickGuide.setContentAreaFilled(false);
The Path you see in Eclipse is relative to the project's root:
Location tag contains the full path you need to provide.
You can also provide relative path to the location where the JVM is launched like this: "./relative/path/to/file"
. Notice the dot in front of the relative path.
If you plan to locate the image inside the jar file use:
new ImageIcon(getClass().getResource("images/quickGuideIcon.png"));
where the above location is classpath relative.
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.