简体   繁体   English

Swing GUI中validate(),revalidate()和invalidate()之间的区别

[英]Difference between validate(), revalidate() and invalidate() in Swing GUI

Swing components have multiple methods related to updates of screen layout, in particular: Swing组件具有与屏幕布局更新有关的多种方法,尤其是:

The Java documentation defines these somewhat from a technical perspective, but it's not particularly clear how they are meant to be used. Java文档从技术角度对这些进行了一些定义,但是尚不清楚如何使用它们。

What is the difference between these, and in what circumstances should you use one rather than the others? 两者之间有什么区别,在什么情况下应使用一种而不是其他方式?

validate() : In Swing when you create a Component, it is not valid ie its valid property is false . validate() :在Swing中创建组件时,该组件valid即其有效属性为false A component is said to be valid, when its width, height, location and stuff has been determined. 确定组件的宽度,高度,位置和填充物后,该组件即为有效组件。 This is usually done by calling their validate() method, directly or indirectly. 这通常是通过直接或间接调用他们的validate()方法来完成的。 When we call validate() on containers, it will validate the container (if it is invalid) by calling its doLayout() method, which typically will invoke the LayoutManager . 当我们在容器上调用validate()时,它将通过调用容器的doLayout()方法(通常会调用LayoutManager )来验证容器(如果无效)。 Now each child placed on this container will be validated recursively, so that the entire tree will be laid out and will become valid. 现在,将递归地验证放置在此容器上的每个子代,以便整个树都将被布局并变得有效。

revalidate() : revalidate() is to be called when you change an attribute that would affect their width/height and call repaint() when you change an attribute that would affect their appearance. revalidate() :当您更改一个会影响宽度/高度的属性时,将调用revalidate()当您更改一个会影响其外观的属性时,将调用repaint()。 For example, if your JFrame contains a JPanel , now at a certain point of time you removed that JPanel and inserted a new one in its place, depending on the contents of the newly placed JPanel , the size of the components inside the JPanel as well as The CONTAINER itself (by virtue of the layout manager used by it), changes. 例如,如果你JFrame包含JPanel ,现在在某一时间点,你删除了JPanel ,并在该位置插入一个新的,这取决于新放置的内容JPanel ,各部件的内部尺寸JPanel以及随着The CONTAINER本身(借助于它使用的布局管理器)的变化。 Which pushes it to the invalid state. 将其推到无效状态。 So in order to validate this change, you have to explicitly call revalidate() . 因此,为了验证此更改,您必须显式调用revalidate()

invalidate() : This is something I have never used, so there might not be much info I can provide about it. invalidate() :这是我从未使用过的东西,因此可能没有太多可提供的信息。 But it seems like the scenarios presented above can give a bit of a hint, as to what happens when using invalidate() . 但是,似乎上面介绍的场景可以提供一些提示,说明使用invalidate()时会发生什么。

invalidate() marks the container as invalid. invalidate()将容器标记为无效。 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() performs relayout. validate()执行重传。 It means invalid content is asked for all the sizes and all the subcomponents' sizes are set to proper values by LayoutManager. 这意味着要求无效的内容的所有大小,并且所有子组件的大小都被LayoutManager设置为适当的值。

revalidate() is just sum of both. revalidate()只是两者的总和。 It marks the container as invalid and performs layout of the container. 它将容器标记为无效并执行容器的布局。

UPDATE: 更新:

Some code from Component.java 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();
        }
    }
}

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

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