I am having trouble getting some components laid out correctly in a game I am making. My GUI consists of an outer JFrame with a BorderLayout, and on the East side of the BorderLayout I have a JPanel with a GridBagLayout. I am trying to position 4 buttons (and eventually some other things--but I cut it down to the buttons when it wasn't working) in the JPanel with their gridx and gridy values set to put them in 2 rows with 2 columns. However, for some reason it isn't working. Instead of putting the buttons in two rows and colums, it is only showing one button (screenshot).
Here is my code:
private class SidePanel extends JPanel
{
private GridLayout sidePaneLayout;
private JButton startButton;
private JButton quitButton;
private JButton saveButton;
private JButton loadButton;
private ButtonHandler buttonHandler;
private JLabel stardate;
private JLabel condition;
private JLabel position;
private JLabel warpFactor;
private JLabel energy;
private JLabel torpedos;
private JLabel shields;
private JLabel klingonsLeft;
private JPanel statusPanel;
public SidePanel()
{
super();
this.setLayout(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
startButton = new JButton("New Game");
constraints.gridx = 0;
constraints.gridy = 0;
this.add(startButton, constraints);
quitButton = new JButton("Quit Game");
constraints.gridx = 0;
constraints.gridy = 1;
this.add(startButton, constraints);
saveButton = new JButton("Save Game");
constraints.gridx = 1;
constraints.gridy = 0;
this.add(startButton, constraints);
loadButton = new JButton("Load Game");
constraints.gridx = 1;
constraints.gridy = 1;
this.add(startButton, constraints);
/*Dimension buttonSize = new Dimension(155, 60);
startButton.setPreferredSize(buttonSize);
quitButton.setPreferredSize(buttonSize);
saveButton.setPreferredSize(buttonSize);
loadButton.setPreferredSize(buttonSize);*/
stardate = new JLabel("Stardate: ");
condition = new JLabel("Condition: ");
position = new JLabel("Position: ");
warpFactor = new JLabel ("Warp Factor: ");
energy = new JLabel("Energy: ");
torpedos = new JLabel("Torpedos: ");
shields = new JLabel("Shields: ");
klingonsLeft = new JLabel("Klingons Left: ");
statusPanel = new JPanel(new GridLayout(0, 2));
statusPanel.add(stardate);
statusPanel.add(condition);
statusPanel.add(position);
statusPanel.add(warpFactor);
statusPanel.add(energy);
statusPanel.add(torpedos);
statusPanel.add(shields);
statusPanel.add(klingonsLeft);
constraints.gridx = 0;
constraints.gridy = 2;
//this.add(statusPanel, constraints);
}
Any idea why this is not working? I've done some other tests with GridBagLayout in standalone JFrames and they all work. But when I try to use it in an interior JPanel in this program, it fails.