我正在制作一个可以从 Internet 加载图片的 JFrame。我有这个工作,但这个 JFrame 的问题是有很多图片,所以它们需要很长时间才能加载。这很好,但我想向用户展示图片正在加载。出于某种原因,我无法让 JPanel 在加载的 JFrame 中显示。我知道这是一个常见错误,我尝试了很多修复,但没有一个有效。这是代码:
final JFrame weatherLoadPop=new JFrame("Loading weather...");
weatherLoadPop.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
weatherLoadPop.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
weatherPop.dispose();
};
});
weatherLoadPop.setResizable(false);
weatherLoadPop.setBounds(100,50,225,100);
JPanel weatherLoadPanel=new JPanel();
weatherLoadPanel.setBackground(Color.RED);
weatherLoadPanel.setPreferredSize(new Dimension(225,100));
JLabel weatherLoadLabel=new JLabel("Loading...0%");
weatherLoadPanel.add(weatherLoadLabel);
weatherLoadPop.add(weatherLoadPanel);
weatherLoadPop.pack();
weatherLoadPop.validate();
weatherLoadPop.setVisible(true);
我不确定我是否正确使用了 pack() 和 validate()。我不经常使用它们。在任何情况下,删除它们都无济于事。对我来说,这个问题最奇怪的部分是加载图片的 JFrame 工作得很好,而更简单的加载 JFrame 却没有。
谢谢你的帮助。