0
votes

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.

2
You will want to create and post a minimal code example program for us to be able to give the best help.Hovercraft Full Of Eels

2 Answers

2
votes

You are adding startButton button 4 times that's why its displaying just single time.

 this.add(startButton, constraints);

Now after some correction it looks like as shown below.

constraints.insets=new Insets(5, 5, 5, 5);// add top, left, bottom, right insets
...
constraints.gridx = 0;
constraints.gridy = 2;
constraints.gridwidth=2; // span two column
this.add(statusPanel, constraints);

enter image description here

0
votes

Change your code to this (gridbag is sensitive if even 1 object is wrong!):

    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(quitButton , constraints);

    saveButton = new JButton("Save Game");
    constraints.gridx = 1;
    constraints.gridy = 0;
    this.add(saveButton , constraints);

    loadButton = new JButton("Load Game");
    constraints.gridx = 1;
    constraints.gridy = 1;
    this.add(loadButton , constraints);