来自JavaDoc :
public void validate()
Validates this container and all of its subcomponents. Validating a container means laying out its subcomponents.
这就是我想做的。使用尽可能轻的组件。但是,当我使用 JComponent
执行此操作时,对 validate()
的调用不会使组件“有效”。
JComponent c = new JComponent() {};
System.out.println(c.isValid()); // false
c.validate();
System.out.println(c.isValid()); // false
为什么我无法使 JComponent
有效?
最佳答案
在 isValid()
的文档中它说:
A component is valid when it is correctly sized and positioned within its parent container and all its children are also valid.
这种情况直到您到达Top-Level Container为止。 (JFrame
、JInternalFrame
或 JApplet
)。在您问题中的示例中,您的 JComponent
没有父级,因此它永远不会有效。
关于java - 为什么我无法验证 JComponent?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8362484/