Swing GUI 中 validate()、revalidate() 和 invalidate() 的区别 [英] Difference between validate(), revalidate() and invalidate() in Swing GUI

查看:29
本文介绍了Swing GUI 中 validate()、revalidate() 和 invalidate() 的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Swing 组件有多种与屏幕布局更新相关的方法,特别是:

Swing components have multiple methods related to updates of screen layout, in particular:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆