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)输出列表?
答案 0 :(得分:8)
System.out.print(thingy)
只是打印方法thingy.toString()
toString()
的{{1}}来自JPanel
类:
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
所以来自toString()
的这一个被调用对于每个JComponent,包括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;
}
的根本原因,它打印在描述JPanel
invalid
@Reimeus
很好地解释了如何实现/**
* 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;
}
状态
答案 1 :(得分:7)
这是isValid()
的java.awt.Component
的结果,由JPanel
扩展:
public boolean isValid()
确定此组件是否有效。组件有效时 它的大小正确并位于其父容器中 它的所有孩子也都有效。为了计算同行的规模 要求,组件在首次显示之前无效 屏幕上。当父容器完全实现时,全部 其组件将有效。
答案 2 :(得分:5)
“无效”字段仅表示需要布置组件。要使'有效'可以通过'打包'父容器来实现,例如:
JPanel p = new JPanel();
JFrame frame = new JFrame();
frame.add(p);
frame.pack();