Code :
import javax.swing.*;
import java.awt.*;
public class firstGUI extends JPanel {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.setVisible(true);
}
public void paintComponent(Graphics g) {
Image image = new ImageIcon("dist.jpg").getImage();
g.drawImage(image,0,0, this);
}
}
Compiles perfectly, but when I run it, it just shows a form. No picture(or any other operation in paintComponent) shows up. Is there something I’m missing?
Your
paintComponentmethod is an instance method of yourfirstGUIclass (aJPanel). The problem is that you are not creating an instance offirstGUIand adding it to the frame.The following replacement
mainmethod instantiatesfirstGUIand adds it to thecontentPaneof the frame: