我的 JLabel 弹出窗口 window 上没有显示任何内容

Nothing is showing up on my pop up window for JLabel

我正在尝试创建一个允许弹出图像和文本的简单脚本。 window 正在弹出,但它是空白的,上面什么也没有。有人知道我做错了什么吗?

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);
        
    }
}

在调用 setVisible(true) 之前调用 frame.pack(); 将确保内部的所有元素在显示之前正确调整大小。

The pack method sizes the frame so that all its contents are at or above their preferred sizes [...]

[...] pack leaves the frame layout manager in charge of the frame size, and layout managers are good at adjusting to platform dependencies and other factors that affect component size.

尽量放

frame.add(label);

首先,之后:

JFrame frame = new JFrame();

像这样:

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