简体   繁体   English

是System.out.print(JPanel的);

[英]System.out.print(JPanel);

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

I have printed out a JPanel, and above is an output. 我打印出一个JPanel,上面是输出。 Can anyone tell me what that "invalid" part means? 谁能告诉我“无效”部分的含义是什么?

Better yet, if anyone knows where to find a list of explaned System.out.print(thingy) outputs for as much thingys as possible? 更好的是,如果有人知道在哪里找到尽可能多的东西的解释System.out.print(thingy)输出的列表?

System.out.print(thingy) is just printing method thingy.toString() System.out.print(thingy)只是打印方法thingy.toString()

toString() for JPanel comes from java.awt.Component class: JPanel toString()来自java.awt.Component类:

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



There is invokation of protected String paramString() that leads us back to JPanel where this method is overridden: 有一个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();
}

and this leads us to JCompomnent that is first level parent class of JPanel (but it still does not have explicitly declared toString() so this one from java.awt.Component is called for every JComponent, including JPanel). 这导致我们JCompomnentJPanel第一级父类(但它仍然没有显式声明toString()因此每个JComponent(包括JPanel)都会调用java.awt.Component这个。
So, paramString() that gets invoked: 所以,调用的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;
}

... and again - super.paramString() - from java.awt.Container ...再次 - 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;
}

... and again - super.paramString() - that finally leads us back to java.awt.Component ... 再次 - 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;
}

And here is the root cause of invalid that was printed inside your string describing JPanel 这是在描述JPanel的字符串中打印的invalid的根本原因

/**
 * 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;
}

How to achieve valid state was nicely explained by @Reimeus @Reimeus很好地解释了如何实现valid状态

It's the result of isValid() of java.awt.Component , which is extended by JPanel : 它是java.awt.ComponentisValid()的结果,它由JPanel扩展:

public boolean isValid() public boolean isValid()

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. 到父容器完全实现时,其所有组件都将有效。

The 'invalid' field simply means that the components need to be laid out. “无效”字段仅表示需要布置组件。 To make 'valid' can be acheived by 'packing' the parent container, eg: 要使'有效'可以通过'打包'父容器来实现,例如:

JPanel p = new JPanel();
JFrame frame = new JFrame();
frame.add(p);
frame.pack();

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

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