2

我创建了一个 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());
             }
        });
    }
}
4

1 回答 1

2
  • JPanelFlowLayout在 API中实现

  • FlowLayout只接受PreferredSize,然后JComponentsinJPanel不可调整大小

  • 看看GridLayout

  • 另一种选择是GridBagLayout或今天的习惯MigLayout

于 2012-11-07T11:38:58.303 回答