Reputation: 763
I am learning to make gui in JAVA, and I am trying to add an image in my JFrame, this is the code I tried:
public class MyApp extends JFrame {
private ImageIcon img;
private JLabel imglabel;
public MyApp(){
setLayout(new FlowLayout());
img = new ImageIcon(getClass().getResource("img.jpg"));
//adding the label for the above Icon
imglabel = new JLabel("this is the image");
add(imglabel);
}
public static void main(String[] args) {
MyApp app = new MyApp();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.pack();
app.setVisible(true);
app.setTitle("reminder");
}
}
but I cannot see any image being displayed on the screen! where did I go wrong?
also the image and the class is in the same directory:
Thanks for the help :)
Upvotes: 2
Views: 529
Reputation: 168815
The icon is never set!
imglabel = new JLabel("this is the image");
Should be..
imglabel = new JLabel("this is the image");
imgLabel.setIcon(img); // or use the 3 arg constructor for JLabel
Upvotes: 2