java swing无法扩展JFrame的高度?

时间:2012-11-07 11:34:58

标签: java swing height layout-manager pack

我创建了一个JFrame并将JPanel设置为其内容面板,我在该容器中添加了大约30个按钮,但JFrame向我显示了一行按钮,它不允许我扩展高度。

public class UIDemo{
    private JPanel contentPanel = new JPanel();
    private JButton buttons[] = new JButton[30];
    public UIDemo(JFrame frame){
        for(int i = 0; i < buttons.length; i++){
           buttons[i] = new JButton("Button"+Integer.toString(i));
           contentPanel.add(buttons[i]);
        }//Add all buttons into content panel.

        frame.setContentPane(contentPanel);
        frame.pack();
        frame.setVisible(true);
    }
    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable(){
             public void run(){
                 new UIDemo(new JFrame());
             }
        });
    }
}

1 个答案:

答案 0 :(得分:2)

  • JPanel已在API

  • 中实施FlowLayout
  • FlowLayout仅接受PreferredSizeJComponents中的JPanel无法调整大小

  • 查看GridLayout

  • 其他选项包括GridBagLayout或今日自定义MigLayout