java - 为什么我无法验证 JComponent?

标签 java swing layout jcomponent

来自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为止。 (JFrameJInternalFrameJApplet)。在您问题中的示例中,您的 JComponent 没有父级,因此它永远不会有效。

关于java - 为什么我无法验证 JComponent?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8362484/

相关文章:

java - 为什么会出现GXT异常以及如何修复?

java - JodaTime 夏令时错误

java.lang.SecurityException : Need BLUETOOTH permission: Neither user 10065 nor current process has android. 权限.BLUETOOTH

android - 为什么即使我更改了 XML 文本的大小,它们也没有改变?

java - IllegalAccessError 仅在导出的签名 APK 上

java - 将 2 个 FlowLayouts 放在另一个 FlowLayouts 下,并留有边距

java - JFrame 可以显示进程吗?

java - org.eclipse.swt.widgets 与 javax.swing.table : Why redefine it?

android - 如何为 ImageView 设置LayoutParams()?

android - 工具栏上的居中标题