更改GridBagLayout中的网格大小

Mat*_*etz 2 java swing gridbaglayout

我只是想在java swing中向GridBagLayout添加3个居中的垂直按钮.现在我相信默认的网格大小是3x3.我想知道我是否可以更改它并添加更多列和行.我只想在一个窗格中制作3个等距间隔的中心.

    pane.setLayout(new GridLayout(10,1));
    // Insert a space before the first button
    for (int i = 1; i < 6; i++ ){
        pane.add(new JLabel(""));
    }
    pane.add(new Button("1"));
    pane.add(new Button("2"));
        pane.add(new Button("3"));
    // Insert a space after the last button
    pane.add(new JLabel(""));
Run Code Online (Sandbox Code Playgroud)

这是最后的解决方案谢谢大家!

GET*_*Tah 6

我同意@Chris GridLayout更容易:

myPanel.setLayout(new GridLayout(5,1));
// Insert a space before the first button
add(new JLabel(""));
add(new Button("1"));
add(new Button("2"));
add(new Button("3"));
// Insert a space after the last button
add(new JLabel(""));
Run Code Online (Sandbox Code Playgroud)