Swing GUI中validate(),revalidate()和invalidate()之间的区别 [英] Difference between validate(), revalidate() and invalidate() in Swing GUI
问题描述
Swing组件有多种与屏幕布局更新相关的方法,特别是:
Swing components have multiple methods related to updates of screen layout, in particular:
validate()
invalidate()
revalidate()
Java文档从技术角度对这些进行了一些定义,但是它们的使用方式并不是特别清楚。
The Java documentation defines these somewhat from a technical perspective, but it's not particularly clear how they are meant to be used.
它们之间的区别是什么?在什么情况下你应该使用一个而不是其他一个?
What is the difference between these, and in what circumstances should you use one rather than the others?
推荐答案
invalidate()
将容器标记为无效。意味着内容在某种程度上是错误的,必须重新布局。但它只是一种标志/旗帜。以后必须刷新多个无效容器。
invalidate()
marks the container as invalid. Means the content is somehow wrong and must be re-laid out. But it's just a kind of mark/flag. It's possible that multiple invalid containers must be refreshed later.
validate()
执行重新布局。这意味着要求所有大小的无效内容,并且所有子组件的大小都由LayoutManager设置为正确的值。
validate()
performs relayout. It means invalid content is asked for all the sizes and all the subcomponents' sizes are set to proper values by LayoutManager.
revalidate()
只是两者的总和。它将容器标记为无效并执行容器的布局。
revalidate()
is just sum of both. It marks the container as invalid and performs layout of the container.
更新:
来自Component的一些代码。 java
Some code from Component.java
public void revalidate() {
revalidateSynchronously();
}
/**
* Revalidates the component synchronously.
*/
final void revalidateSynchronously() {
synchronized (getTreeLock()) {
invalidate();
Container root = getContainer();
if (root == null) {
// There's no parents. Just validate itself.
validate();
} else {
while (!root.isValidateRoot()) {
if (root.getContainer() == null) {
// If there's no validate roots, we'll validate the
// topmost container
break;
}
root = root.getContainer();
}
root.validate();
}
}
}
这篇关于Swing GUI中validate(),revalidate()和invalidate()之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!