Search code examples
javaswingjframejlabel

Nothing is showing up on my pop up window for JLabel


I'm trying to create a simple script that will allow a pop up of images and text. The window is poping up but its blank with nothing on it. Anyone know what I am doing wrong?

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.*;

public class Thermomter {
    public static void main(String[] args) {
        ImageIcon cold = new ImageIcon("cold_thermomtor.png");

        JLabel label = new JLabel();
        label.setIcon(cold);
        label.setText("omg this is painfull");
            
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500, 500);
        frame.setVisible(true);
        frame.add(label);
        
    }
}

Solution

  • Try to put

    frame.add(label);
    

    first, after:

    JFrame frame = new JFrame();
    

    Like this:

    JFrame frame = new JFrame();
    frame.add(label);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500, 500);
    frame.setVisible(true);