My program will run, and a button will appear, but the image which is in the res folder that is in a referenced library will not appear ( egg.png
). There are no errors, but I'm confused as to why it won't run properly.
package gui;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
public class IconButton {
public static void main(String args[]) {
JFrame frame = new JFrame("DefaultButton");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ImageIcon image = new ImageIcon("C:\\Users\\Jack Young\\Desktop\\Egg game Sprites");
JButton button = new JButton();
button.setIcon(image);
frame.add(button);
frame.setSize(300, 200);
frame.setResizable(false);
frame.setVisible(true);
}
}
try this:- for eg:- if image is in icons folder in resource:-
private final ImageIcon imageIcon = new ImageIcon(getClass().getClassLoader().getResource("icons/image.png"));
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.