System.out.print(thingy)
ただの印刷方法ですthingy.toString()
toString()
のためにクラス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
がの第1レベルの親クラスになりますJPanel
(ただし、まだ明示的に宣言されていないtoString()
ため、このクラスはjava.awt.Component
JPanelを含むすべてのJComponentに対して呼び出されます)。
だから、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によってうまく説明されました