简体   繁体   English

如何刷新JFrame?

[英]How to refresh a JFrame?

So in my program I set in the beginning a JLabel to it so now an image is on it. 因此,在我的程序中,我在开始时就为其设置了JLabel,因此现在上面有一个图像。 Now I need to remove that JLabel and put a new one instead of it. 现在,我需要删除该JLabel并放置一个新的JLabel。 So I did: 所以我做了:

frame.remove(test_loadingJL);
frame.add(test_lockScreenJL);

But when I look, it is still the first image and it doesn't update until I resize the actual window manually. 但是当我看时,它仍然是第一张图像,并且直到我手动调整实际窗口的大小时它才会更新。 Is there a way to update the frame so I don't have to do that? 有没有更新框架的方法,所以我不必这样做?

这应该可以工作frame.revalidate()

Your Choices: 您的选择:

 1.SwingUtilities.updateComponentTreeUI(name of Component(in your example is frame)

SwingUtilities.updateComponentTreeUI(frame);

2.frame.dispose();

frame.setVisible(true);

3.revalidate();

  revalidate();//<------------needed when add/remove components
  repaint();//sometimes needed also, sometimes not

Also check this link possible dublicate question same question here 在这里也检查此链接可能重复的问题相同的问题

These should work 这些应该工作

在添加组件之前,请尝试调用frame.setVisible(true)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM