java Container.validate() 方法有什么作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1607899/
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
What does Container.validate() method do?
提问by amit
There seems to be many methods in Java awt Containerclass that are related to validate. Apparently they don't do data validation. Is it useful for a Swing developer in any cases? Some of the methods: validate(), invalidate(), validateTree(), isValid() etc.
Java awt Container类中似乎有很多方法与validate相关。显然他们不做数据验证。在任何情况下,它对 Swing 开发人员有用吗?一些方法:validate()、invalidate()、validateTree()、isValid()等。
回答by Michael Borgwardt
Citing the API doc:
引用API 文档:
The validate method is used to cause a container to lay out its subcomponents again. It should be invoked when this container's subcomponents are modified (added to or removed from the container, or layout-related information changed) after the container has been displayed.
validate 方法用于使容器重新布置其子组件。它应该在容器显示后修改(添加到容器或从容器中删除,或更改布局相关信息)时调用此容器的子组件。
回答by Adamski
Validation in a Swing context concerns requesting a component to lay-out its sub-components after one of these is modified.
Swing 上下文中的验证涉及请求组件在修改其中一个子组件后对其子组件进行布局。
For example, suppose you implement a custom JDialog with a button "Show Filters". Upon clicking this button, you might want to add an additional "filter" panel to the south of the JDialog. Upon adding the new sub-panel you would be required to call validate() on the JDialog to cause it to lay-out the new panel correctly.
例如,假设您实现了一个带有“显示过滤器”按钮的自定义 JDialog。单击此按钮后,您可能希望在 JDialog 的南部添加一个额外的“过滤器”面板。添加新的子面板后,您需要在 JDialog 上调用 validate() 以使其正确布局新面板。

