Grid bag layout not displaying the way I want

房东的猫 提交于 2019-12-01 00:01:54
nIcE cOw

As you wrote c.gridx = 0 and c.gridy = 0, you forgot to specify how many columns you want this JPanel to occupy. In order to specify that, you should be using c.gridwidth = 2, which tells the layout that this JPanel needs space for two columns.

To get an output like this:

Here is a small working example:

import java.awt.*;
import javax.swing.*;
// http://stackoverflow.com/questions/10968853/two-jpanels-in-jframe-one-under-other
public class GridBagLayoutExample
{
    // http://stackoverflow.com/questions/10977017/grid-bag-layout-not-displaying-the-way-i-want
    private void displayGUI()
    {
        JFrame frame = new JFrame("GridBagLayout Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel contentPane = new JPanel();
        contentPane.setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.FIRST_LINE_START;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 2;
        gbc.weightx = 0.5;
        gbc.weighty = 0.2;

        JPanel topLeftPanel = new JPanel();
        topLeftPanel.setOpaque(true);
        topLeftPanel.setBackground(Color.DARK_GRAY);
        contentPane.add(topLeftPanel, gbc);

        gbc.gridx = 2;

        JPanel topRightPanel = new JPanel();
        topRightPanel.setOpaque(true);  
        topRightPanel.setBackground(Color.BLUE);
        contentPane.add(topRightPanel, gbc);

        gbc.gridwidth = 1;
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.weightx = 0.25;
        gbc.weighty = 0.8;

        JPanel firstPanel = new JPanel();
        firstPanel.setOpaque(true);
        firstPanel.setBackground(Color.RED);
        contentPane.add(firstPanel, gbc);

        gbc.gridx = 1;

        JPanel secondPanel = new JPanel();
        secondPanel.setOpaque(true);
        secondPanel.setBackground(Color.GREEN.darker());
        contentPane.add(secondPanel, gbc);

        gbc.gridx = 2;

        JPanel thirdPanel = new JPanel();
        thirdPanel.setOpaque(true);
        thirdPanel.setBackground(Color.WHITE);
        contentPane.add(thirdPanel, gbc);

        gbc.gridx = 3;

        JPanel fourthPanel = new JPanel();
        fourthPanel.setOpaque(true);
        fourthPanel.setBackground(Color.MAGENTA);
        contentPane.add(fourthPanel, gbc);

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

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new GridBagLayoutExample().displayGUI();
            }           
        });
    }
}

Here is (another) alternative nested layout. Panels 3 through 6 will get extra height, the extra width will be divided evenly amongst all 6 panels.

import java.awt.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

public class SixPanelNested {

    SixPanelNested() {
        JPanel gui = new JPanel(new BorderLayout());
        gui.setBorder(new TitledBorder("BorderLayout()"));

        JPanel north = new JPanel(new GridLayout(1,0));
        north.setBorder(new TitledBorder("GridLayout(1,0)"));
        gui.add(north, BorderLayout.NORTH);
        for (int ii=1; ii<3; ii++) {
            JLabel l = new JLabel("Panel " + ii);
            l.setBorder(new LineBorder(Color.BLACK));
            north.add(l);
        }

        JPanel south = new JPanel(new GridLayout(1,0));
        south.setBorder(new TitledBorder("GridLayout(1,0)"));
        gui.add(south);
        for (int ii=3; ii<7; ii++) {
            JLabel l = new JLabel("Panel " + ii);
            l.setBorder(new LineBorder(Color.BLACK));
            south.add(l);
        }

        JOptionPane.showMessageDialog(null, gui);
    }

    public static void main(String[] args) throws Exception {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new SixPanelNested();
            }
        });
    }
}

I am not familiar enough with the GridBagLayout to quickly spot what you can improve to your code to fix this. But another approach might be possible, using nested BorderLayouts.

Panels 'first', 'third' and 'fourth' can be placed inside one JPanel Awith a BorderLayout in the NORTH, WEST and EAST respectively. The same can be done for the panels 'second', 'fifth' and 'sixth' in a JPanel B.

Then you group those 2 panels (A and B) in another panel with a BorderLayout in the WEST and EAST

Add this:

c.gridwidth = 2;

Before adding the FIRST and SECOND panels, then set gridwidth back to 1 before adding the others. Alternatively you could separate them further as suggested above. You could use two FlowLayouts for each row, and add those two to another panel with a BorderLayout with NORTH/SOUTH.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!