GridBagLayout is not working

2019-01-20 18:01发布

问题:

this.rootComponent.setLayout(new GridBagLayout());
        GridBagConstraints gbc=new GridBagConstraints();

        //gbc.gridwidth=2;
        gbc.gridx=0;
        gbc.gridy=0;
        gbc.gridwidth=8;
        gbc.anchor=GridBagConstraints.FIRST_LINE_START;

        this.rootComponent.add(new JLabel("Test label 1"),gbc);

        gbc.gridx=8;
        gbc.gridy=12;
        gbc.gridwidth=GridBagConstraints.REMAINDER;
        gbc.anchor=GridBagConstraints.FIRST_LINE_START;

        this.rootComponent.add(new JLabel("Test label"),gbc);

Want to format it like this. grey part shows the jpanel part. Initially i want to layout the first 2 jpanel correctly . which is not working. how to fix it?

回答1:

You are failing to specify any weightx and weighty values to the GridBagConstraints. Moreover your gridwidth values are wrong, since it only needs to be 2 for the bottom most JPanel, for the rest it needs to be 1.


Explanation of what I am doing : Consider JPanels BLUE and RED, they are to be placed along the X-AXIS, in the ratio 70:30, with respect to each other (therefore their weightx will be 0.7 and 0.3 respectively. Since the total area along the X-AXIS is 1.0).

Now both of these BLUE and RED JPanels are to be placed along the Y-AXIS, with respect to the third GREEN JPanel in the ratio 90:10, therefore, both of these BLUE and RED will have weighty = 0.9, and the GREEN JPanel will have weighty = 0.1, but since GREEN JPanel is suppose to occupy the whole area (with respect to X-AXIS), as occupied by BLUE and RED JPanels, for that matter, its gridwidth = 2 and weightx = 1.0.


Try this code example :

import java.awt.*;
import javax.swing.*;

public class GridBagLayoutExample
{
    private GridBagConstraints gbc;

    public GridBagLayoutExample()
    {
        gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.FIRST_LINE_START;
    }

    private void displayGUI()
    {
        JFrame frame = new JFrame("GridBagLayout Example");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel contentPane = getPanel(Color.WHITE);
        contentPane.setLayout(new GridBagLayout());

        JPanel leftPanel = getPanel(Color.BLUE);
        JPanel rightPanel = getPanel(Color.RED);
        JPanel bottomPanel = getPanel(Color.GREEN.darker());

        addComp(contentPane, leftPanel
                , 0, 0, 0.7, 0.9, 1, 1, GridBagConstraints.BOTH);
        addComp(contentPane, rightPanel
                , 1, 0, 0.3, 0.9, 1, 1, GridBagConstraints.BOTH);
        addComp(contentPane, bottomPanel
                , 0, 1, 1.0, 0.1, 2, 1, GridBagConstraints.BOTH);

        frame.setContentPane(contentPane);
        //frame.pack();
        frame.setSize(300, 300);
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private void addComp(JPanel panel, JComponent comp
                            , int gridX, int gridY
                            , double weightX, double weightY
                            , int gridWidth, int gridHeight, int fill)
    {
        gbc.gridx = gridX;
        gbc.gridy = gridY;
        gbc.weightx = weightX;
        gbc.weighty = weightY;
        gbc.gridwidth = gridWidth;
        gbc.gridheight = gridHeight;
        gbc.fill = fill;

        panel.add(comp, gbc);
    }

    private JPanel getPanel(Color backColour)
    {
        JPanel panel = new JPanel();
        panel.setOpaque(true);
        panel.setBackground(backColour);
        panel.setBorder(
            BorderFactory.createEmptyBorder(5, 5, 5, 5));

        return panel;
    }

    public static void main(String[] args)
    {
        Runnable runnable = new Runnable()
        {
            @Override
            public void run()
            {
                new GridBagLayoutExample().displayGUI();
            }
        };
        EventQueue.invokeLater(runnable);
    }
}

Here is the OUTPUT of the same :