什么是.pack()呢? [英] What does .pack() do?
本文介绍了什么是.pack()呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在学习有关的JPanel和网格布局,这个片段code会产生6键
I am learning about JPanel and GridLayout , this snippet of code will produce a simple JPanel with 6 buttons
package testing;
import java.io.*;
import java.util.*;
import java.security.*;
import javax.xml.bind.DatatypeConverter;
import java.lang.*;
import java.awt.*;
import javax.swing.*;
public class Testing
{
public static class GridPanel extends JPanel
{
public GridPanel()
{
setLayout(new GridLayout(2,3));
setBackground(Color.GREEN);
this.setPreferredSize(new Dimension(500,500));
JButton b1 = new JButton ("Button 1");
JButton b2 = new JButton ("Button 2");
JButton b3 = new JButton ("Button 3");
JButton b4 = new JButton ("Button 4");
JButton b5 = new JButton ("Button 5");
JButton b6 = new JButton ("Button 6");
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
add(b6);
}
}
public static void main(String[] args)
{
GridPanel gp = new GridPanel();
JFrame jf = new JFrame();
jf.add(gp);
jf.pack(); //code wouldnt work if i comment out this line
jf.setVisible(true);
}
}
我想知道为什么如果我注释掉 jf.pack()我的code难道不工作
推荐答案
的的包大小的方法框架,使所有的内容在或其preferred规模以上。收拾另一种方法是通过调用的setSize或的setBounds(其中还设置帧位置)建立明确的帧大小。一般情况下,使用组是preferable调用的setSize,因为包叶负责帧大小的框架布局管理器,和布局管理器善于适应的平台依赖性和影响元件尺寸的其他因素。的
您还应该参考的Javadoc任何时间你需要在任何Java API的更多信息
You should also refer to Javadocs any time you need additional information on any Java API
这篇关于什么是.pack()呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文