简体   繁体   中英

Java: Position JButtons in JFrame/JPanel

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();
}

} 在此处输入图片说明

A common approach is to make a JPanel with a LayoutManager of BorderLayout. You would put all of the contents of the panel (which could be a collection of subpanels) in BorderLayout.CENTER, and put the panel containing just your buttons in BorderLayout.SOUTH.

See: How to Use Layouts and How To Use BorderLayout

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM