Java 添加新组件后刷新 JFrame

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3718435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 03:54:01  来源:igfitidea点击:

refresh JFrame after adding new Components

javauser-interfacejframe

提问by reox

I want to add some new Components to my JFrame during runtime when a button is pressed. This works so far, but i have to resize the window manually to see the new components.

我想在运行时按下按钮时向我的 JFrame 添加一些新组件。到目前为止,这有效,但我必须手动调整窗口大小以查看新组件。

Is there any Action I can fire or a method to call to refresh the window? Any help appreciated. Thanks in advance.

是否有任何我可以触发的操作或调用刷新窗口的方法?任何帮助表示赞赏。提前致谢。

采纳答案by crusam

You have to revalidate();the frame. If that doesn't work you also have to call repaint();

你必须到revalidate();框架。如果这不起作用,您还必须致电repaint();

回答by dogbane

Call

称呼

revalidate();
repaint();

revalidatetells the layout manager to reset based on the new component list. This will also trigger a call to repaint.

revalidate告诉布局管理器根据新的组件列表重置。这也会触发重绘调用。

repaintis used to tell a component to repaint itself.

repaint用于告诉组件重新绘制自身。

回答by A.YULGHUN

in java repaint();working for me. while revalidate();not working for me.

在java中repaint();为我工作。虽然revalidate();不为我工作。