请教关于Java小例子,pack()是什么意思

来源:百度知道 编辑:UC知道 时间:2024/06/18 05:40:50
下面是书上的小例子 下面有一行 f.pack() 是什么意思
import java.awt.*;

public class ExGui
{
private Frame f;
private Button b1;
private Button b2;

public static void main(String[] args)
{
ExGui that=new ExGui();
that.go();
}
public void go()
{
f=new Frame("GUI example");
f.setLayout(new FlowLayout());
b1=new Button("Press Me");
b2=new Button("Don't Press Me");
f.add(b1);
f.add(b2);
f.pack();//这一行是什么意思啊
f.setVisible(true);
}
}

建议看一下java文档:

javaDoc:
调整此窗口的大小,以适合其子组件的首选大小和布局。

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