简体   繁体   English

如何在JComponent.revalidate和Container.validate之间选择

[英]how do I choose between JComponent.revalidate and Container.validate

I am using WindowBuilder and Swing. 我正在使用WindowBuilder和Swing。 I am performing the following on a Swing JPanel: 我在Swing JPanel上执行以下操作:

java.awt.Container.setLayout()
java.awt.Container.removeAll()
java.awt.Container.add()

To finish I invoke: 最后,我调用:

java.awt.Container.validate() but I noticed that javax.swing.JComponent.revalidate() seems to do the same thing in practice. java.awt.Container.validate()但我注意到javax.swing.JComponent.revalidate()在实践中似乎做同样的事情。 I have not observed any difference in behaviour. 我没有观察到任何行为上的差异。 JPanel inherits from both Container and JComponent. JPanel继承自Container和JComponent。 How do I choose between them? 我该如何选择呢?

3 important differences: 3个重要区别:

  • JComponent#revalidate() invalidates the component first which Container#validate() does not do. JComponent#revalidate()首先使Container#validate()不执行的组件无效。
  • JComponent#revalidate() does not validate immediately, it adds the component to a list of invalid components and the RepaintManager will validate components in batches. JComponent#revalidate()不会立即进行验证,它会将组件添加到无效组件列表中,并且RepaintManager将批量验证组件。 This can improve performance when lots of validations are required. 当需要大量验证时,这可以提高性能。
  • JComponent#revalidate() can be called from any thread, not just the event dispatch thread. 可以从任何线程调用JComponent#revalidate() ,而不仅仅是事件分发线程。

Unless you need an immediate effect (which is only needed in special situations), JComponent#revalidate() is preferrable. 除非您需要立即生效(仅在特殊情况下才需要),否则最好使用JComponent#revalidate()

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

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