Question

I'm trying to make a simple menu for my game. I have 4 buttons in the center and I want to make them a little bit bigger and center them.

The last part worked but I can't seem to call any of my JButtons and do a .setSize / .setPreferedSize(new Dimension()) on it.

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");
    start.setBackground(Color.gray);
    highscore = new JButton("Higscores");
    help = new JButton("Help");
    stoppen = new JButton("Stoppen");
}

private void maakLayout() {
    JPanel hoofdmenu = new JPanel();
    hoofdmenu.setLayout(new BoxLayout(hoofdmenu, BoxLayout.Y_AXIS ));
    hoofdmenu.add(start);
    start.setAlignmentX(CENTER_ALIGNMENT);
    hoofdmenu.add(highscore);
    highscore.setAlignmentX(CENTER_ALIGNMENT);
    hoofdmenu.add(help);
    help.setAlignmentX(CENTER_ALIGNMENT);
    hoofdmenu.add(stoppen);
    stoppen.setAlignmentX(CENTER_ALIGNMENT);
    super.add(hoofdmenu);
}

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

}

public static void main(String[] args) {
    new mainMenu();
}
}
Was it helpful?

Solution

As an example, to change the size of the "Start" button,

change :

    start1 = new JButton("Start");

to

    start1 = new JButton("Start") {
        {
            setSize(150, 75);
            setMaximumSize(getSize());
        }
    };

OTHER TIPS

The problem is that JFrames use BorderLayout by default, which means that your JPanel will naturally fill the space.

Before adding your JPanel, call the following code to change the JFrame's layout to null and use the JPanel's settings instead.

this.setLayout(null);
JPanel hoofdmenu = new JPanel();
hoofdmenu.setBounds(0,0, 400, 100);

Alternatively, you could set the maximum size of the JButtons

Dimension maxSize = new Dimension(100, 100);
start.setMaximumSize(maxSize);
highscore.setMaximumSize(maxSize);
help.setMaximumSize(maxSize);
stoppen.setMaximumSize(maxSize);

Here's another example following behind the previous two - I'm making a soundboard program, and this is actually a sample from it - The JPanel actually is needed, agreeing to the second post.

    JFrame frame = new JFrame();
    JPanel menuPanel = new JPanel();
    JButton Button1 = new JButton("<BUTTON NAME 1>");
    Button1.setSize(80, 30);
    Button1.setLocation(4, 4);
    JButton Button2 = new JButton("<BUTTON NAME 2>");
    Button2.setSize(80, 30);
    Button2.setLocation(90, 4);

Ah, and another thing - You created the buttons in a different block from the second piece of code. Doing that causes the other blocks to not see it. You need to declare them outside the block so all the blocks can see them.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top