2
core.EditArea[,119,96,556x931,invalid,layout=javax.swing.BoxLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=9,maximumSize=,minimumSize=,preferredSize=]

我打印了一个JPanel,上面是一个输出。谁能告诉我那个“无效”部分是什么意思?

更好的是,如果有人知道在哪里可以找到尽可能多的东西的解释 System.out.print(thingy) 输出列表?

4

3 回答 3

8

System.out.print(thingy)只是打印方法thingy.toString()

toString()forJPanel来自java.awt.Component类:

public String toString() {
    return getClass().getName() + "[" + paramString() + "]";
}



调用protected String paramString()它会导致我们回到JPanel这个方法被覆盖的地方:

/**
 * Returns a string representation of this JPanel. This method 
 * is intended to be used only for debugging purposes, and the 
 * content and format of the returned string may vary between      
 * implementations. The returned string may be empty but may not 
 * be <code>null</code>.
 * 
 * @return  a string representation of this JPanel.
 */
protected String paramString() {
    return super.paramString();
}

这使我们JCompomnent看到了它的第一级父类JPanel(但它仍然没有显式声明,因此每个 JComponent 都会调用toString()这个 from ,包括 JPanel)。 所以,它被调用:java.awt.Component
paramString()

protected String paramString() {
    String preferredSizeString = (isPreferredSizeSet() ?
                  getPreferredSize().toString() : "");
    String minimumSizeString = (isMinimumSizeSet() ?
                getMinimumSize().toString() : "");
    String maximumSizeString = (isMaximumSizeSet() ?
                getMaximumSize().toString() : "");
    String borderString = (border != null ?
               border.toString() : "");

    return super.paramString() +
    ",alignmentX=" + alignmentX +
    ",alignmentY=" + alignmentY +
    ",border=" + borderString +
",flags=" + flags +             // should beef this up a bit
    ",maximumSize=" + maximumSizeString +
    ",minimumSize=" + minimumSizeString +
    ",preferredSize=" + preferredSizeString;
}

......再一次 - super.paramString()- 从java.awt.Container

protected String paramString() {
String str = super.paramString();
LayoutManager layoutMgr = this.layoutMgr;
if (layoutMgr != null) {
    str += ",layout=" + layoutMgr.getClass().getName();
}
return str;
}

......再一次- super.paramString()- 这最终让我们回到java.awt.Component

protected String paramString() {
    String thisName = getName();
    String str = (thisName != null? thisName : "") + "," + x + "," + y + "," + width + "x" + height;
    if (!isValid()) {
        str += ",invalid";
    }
    if (!visible) {
        str += ",hidden";
    }
    if (!enabled) {
        str += ",disabled";
    }
    return str;
}

invalid这是打印在描述 JPanel 的字符串中的根本原因

/**
 * Determines whether this component is valid. A component is valid
 * when it is correctly sized and positioned within its parent
 * container and all its children are also valid. 
 * In order to account for peers' size requirements, components are invalidated
 * before they are first shown on the screen. By the time the parent container 
 * is fully realized, all its components will be valid.
 * @return <code>true</code> if the component is valid, <code>false</code>
 * otherwise
 * @see #validate
 * @see #invalidate
 * @since JDK1.0
 */
public boolean isValid() {
    return (peer != null) && valid;
}

valid@Reimeus 很好地解释了如何实现状态

于 2012-08-19T00:08:40.873 回答
7

它是 的结果,isValid()java.awt.Component扩展JPanel

公共布尔 isValid()

确定此组件是否有效。当一个组件在其父容器中的大小和位置正确并且其所有子容器也有效时,它是有效的。为了考虑对等体的大小要求,组件在首次显示在屏幕上之前就无效了。当父容器完全实现时,它的所有组件都将有效。

于 2012-08-19T00:11:42.500 回答
5

“无效”字段仅表示需要布置组件。可以通过“打包”父容器来实现“有效”,例如:

JPanel p = new JPanel();
JFrame frame = new JFrame();
frame.add(p);
frame.pack();
于 2012-08-19T00:18:52.093 回答