pack()方法,该如何解决

pack()方法在做awt相关的练习时有时用到pack()方法,想知道这个方法的作用是什么呢?什么时候要用这个方法呢

pack()方法
在做awt相关的练习时有时用到pack()方法,想知道这个方法的作用是什么呢?什么时候要用这个方法呢?

[解决办法]
关于pack()方法 
在 Frame 类中有一个从类 java.awt.Window 继承的方法 pack() 
show() 同样也继承自 java.awt.Window 

public void pack() 
调整此窗口的大小,以适合其子组件的首选大小和布局。如果该窗口和/或其所有者仍不可显示,则两者在计算首选大小之前变得可显示。在计算首选大小之后,将会验证该 Window。
[解决办法]
public void pack()
Causes this Window to be sized to fit the preferred size and layouts of its subcomponents. If the window and/or its owner are not yet displayable, both are made displayable before calculating the preferred size. The Window will be validated after the preferredSize is calculated. 


意思就是说,自动调整窗口首选大小和布局,去适应该窗口的容器组建。如果这个窗口和它的容器之前没有显示出来的话,在计算首选大小之前,先显示出它们。那样的话,在首选大小被计算出来之后,就能正确显示了...
[解决办法]
一般在你将控件放置在容器之后,调用这个pack()方法...


给分吧 呵呵
[解决办法]
javaDoc:
调整此窗口的大小,以适合其子组件的首选大小和布局。
javaSrc:

Java code
/**     * Causes this Window to be sized to fit the preferred size     * and layouts of its subcomponents.  If the window and/or its owner     * are not yet displayable, both are made displayable before     * calculating the preferred size.  The Window will be validated     * after the preferredSize is calculated.     * @see Component#isDisplayable     */    public void pack() {    Container parent = this.parent;    if (parent != null && parent.getPeer() == null) {        parent.addNotify();    }    if (peer == null) {        addNotify();    }        Dimension newSize = getPreferredSize();        if (peer != null) {            setClientSize(newSize.width, newSize.height);        }        if(beforeFirstShow) {            isPacked = true;        }    validate();    }