请教关于Java小例子,pack()是什么意思
下面是书上的小例子下面有一行f.pack()是什么意思importjava.awt.*;publicclassExGui{privateFramef;privateBut...
下面是书上的小例子 下面有一行 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);
}
} 展开
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;
}
validate();
}
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;
}
validate();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询