user7460099 user7460099 -4 years ago 118
Java Question

Placing button panel in center Java Swing

I am new to swing and trying to make a simple quiz game just for practice reasons. What I want to make is like this: enter image description here

This was my first implementation but there I extends JFrame and make all of this in one frame then i realize i need to use cardlayout to change from the main menu to the playing scene so i split it to 1 class main frame and other classes for the mainmenu, play scene, game over scene, etc. So my MainMenu now extends JPanel and when I add buttons to the button panel and then add buttonpanel to the mainpanel i got this:

enter image description here

Its like the panel containing label of the main menu and the button panel are next to each other but i need them like in the first picture. Here is my MainMenu class:

public class MainMenu extends JPanel{

private JLabel menuTitle;
private JPanel menuTitlePanel;
private JPanel buttonPanel;

public MainMenu(){

this.setBackground(new Color(0, 128, 43));

//ADDING THE TITLE
menuTitle = new JLabel("<html><h1><strong><i>Krisko Beatz Quiz</i></strong></h1><hr></html>");
menuTitle.setForeground(Color.BLACK);

menuTitlePanel = new JPanel(new GridBagLayout());
menuTitlePanel.setBackground(new Color(0, 128, 43));

GridBagConstraints gbcTitle = new GridBagConstraints();
gbcTitle.weightx = 0;
gbcTitle.weighty = 0;
gbcTitle.gridx = 0;
gbcTitle.gridy = 0;
gbcTitle.gridwidth = 3;
gbcTitle.insets = new Insets(70, 0, 0, 0);

menuTitlePanel.add(menuTitle, gbcTitle);
this.add(menuTitlePanel, BorderLayout.NORTH);

buttonPanel = new JPanel(new GridBagLayout());
buttonPanel.setBackground(new Color(0, 128, 43));

JButton startButton = new JButton("Start");

GridBagConstraints gbcStart = new GridBagConstraints();
gbcStart.gridx = 1;
gbcStart.gridy = 1;
gbcStart.ipadx = 50;
gbcStart.ipady = 10;
gbcStart.gridwidth = 3;
gbcStart.insets = new Insets(10, 0, 0, 0);

buttonPanel.add(startButton, gbcStart);
this.add(buttonPanel, BorderLayout.CENTER);



}
}


Any help will be appreciated.

Answer Source

So, you basically have two basic groups, the title and the buttons, these two need to be managed individually, as they have different layout requirements (primarily, the buttons been laid out in the middle).

Menu

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;

public class TestMenu {

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

    public TestMenu() {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame("Test");
                frame.add(new MenuPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class MenuPane extends JPanel {

        public MenuPane() {
            setBorder(new EmptyBorder(10, 10, 10, 10));
            setLayout(new GridBagLayout());

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.anchor = GridBagConstraints.NORTH;

            add(new JLabel("<html><h1><strong><i>Krisko Beatz Quiz</i></strong></h1><hr></html>"), gbc);

            gbc.anchor = GridBagConstraints.CENTER;
            gbc.fill = GridBagConstraints.HORIZONTAL;

            JPanel buttons = new JPanel(new GridBagLayout());
            buttons.add(new JButton("Start"), gbc);
            buttons.add(new JButton("Show scores"), gbc);
            buttons.add(new JButton("Help"), gbc);
            buttons.add(new JButton("Exit"), gbc);

            gbc.weighty = 1;
            add(buttons, gbc);
        }

    }

}

That should help with the current question, but you'll probably also want to have a look at How to use CardLayout for details about how you could switch between different views

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download