12

我有一个摆动应用程序,我在 JPanel 中显示图像。如果应用程序无法生成图像,我想从 JPanel 中删除前一个图像并将其替换为 JTextField 和消息。我可以添加 text field ,但它是在前面的内容之上绘制的,它本身就是 JPanel 的子类。这是我所拥有的:

private void displayMessage(String message) {
  JTextField tf = new JTextField(message);
  cdPanel.removeAll();
  cdPanel.add(tf, BorderLayout.NORTH);//tried lots of variations, inc. no layout    
  cdPanel.validate();
}

我怎样才能让 cdPanel 完全重绘自己?

4

2 回答 2

28

您可以简单地尝试调用:

cdPanel.revalidate();
cdPanel.repaint();   // This is required in some cases

代替

cdPanel.validate();
于 2013-08-01T18:21:28.693 回答
3

As you are dealing with unpredictable latency, use a SwingWorker to do the loading in the background, as shown here. The example uses pack() to resize the label to that of the image, but you may want to use a fixed-size grid and scale the images, as shown here.

于 2013-08-01T18:06:47.473 回答