1

Hi I made some buttons align nicely next to each other but now I want them at the bottom of my panel/frame.

How can I do this? I thought about setalignment but that doesn't seem to work.

Thanks in advance:

public class mainMenu extends JFrame {
private JButton start, highscore, help, stoppen;


public mainMenu() {
    super("Master Mind");
    maakComponenten();
    maakLayout();
    toonFrame();

}

private void maakComponenten() {

    start = new JButton("Start") {
        {
            setSize(150, 30);
            setMaximumSize(getSize());
        }
    };
    highscore = new JButton("Highscore") {
        {
            setSize(150, 30);
            setMaximumSize(getSize());
        }
    };
    help = new JButton("Help") {
        {
            setSize(150, 30);
            setMaximumSize(getSize());
        }
    };
    stoppen = new JButton("Stoppen") {
        {
            setSize(150, 30);
            setMaximumSize(getSize());
        }
    };

}

private void maakLayout() {

    JPanel hoofdmenu = new JPanel();
    hoofdmenu.setLayout(new BoxLayout(hoofdmenu, BoxLayout.X_AXIS));
    hoofdmenu.add(start);
    hoofdmenu.add(highscore);
    hoofdmenu.add(help);
    hoofdmenu.add(stoppen);
    super.add(hoofdmenu);


}

private void toonFrame() {
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    setVisible(true);
    setSize(500, 500);

}

public static void main(String[] args) {
    new mainMenu();
}

}enter image description here

4

1 回答 1

5

一种常见的方法是使用 BorderLayout 的 LayoutManager 制作 JPanel。您可以将面板的所有内容(可能是子面板的集合)放在 BorderLayout.CENTER 中,并将仅包含按钮的面板放在 BorderLayout.SOUTH 中。

请参阅:如何使用布局如何使用 BorderLayout

于 2013-03-13T20:36:55.017 回答