gridheight moves component on top of each other

2019-09-17 09:31发布

问题:

I have a JFrame which consists mainly of a left and right side. The left has 2 components 1 above the other, and the right has 9, again 1 on top of each other. As the left has 2, I want 1 component to equal the same vertical space as 6 on the right. I am using the gridBagConstraints layout and have each JPanel positioned in the main JFrame. Everything looks OK (apart from what I was just saying). To sort this problem I use c.gridHeight = 6 on the JPanel on the left. However this then puts the 6 JPanels which are on the right ontop of each other, ignoring their formatting. How can I rectify this problem? Snipets of my code which still cause the same problem are:

void create()
{
    JFrame screen = new JFrame("Dungeon of Doom");
    screen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    screen.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    screen.setSize(new Dimension(screenWidth, screenHeight));

    screen.setPreferredSize(new Dimension(screenWidth, screenHeight));

    //Add all JPanes to screen
    c.gridy = 0;
    c.gridx = 0;
    c.gridheight = 6;

    screen.add(lookReply(), c);


    c.gridy = 0;
    c.gridx = 1;
    screen.add(title(), c);

    c.gridy = 1;
    c.gridx = 1;
    screen.add(space(), c);



    c.gridy = 2;
    c.gridx = 1;
    screen.add(commands(), c);

            //...So on, same for others

}

A sample method - the JPanel - all methods have the same content just different titles and less/more buttons/labels

private JPanel title()
{       

    JPanel titlePanel = new JPanel(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    titlePanel.setBackground(Color.red);
    titlePanel.setOpaque(true);


    JLabel title = new JLabel("DOD");
    titlePanel.add(title, c);


    return titlePanel;
}

I put a background on the JPanels just to help see where they are in the window

All help appreciated! Thanks :)