如何使用GridBagLayout维护jpanel的尺寸大小?

时间:2014-01-20 11:21:06

标签: java swing resize jpanel gridbaglayout

这是我的代码。当我将程序的大小调整为其他布局时,除CheckPanelGridBagLayout)外,它的工作正常;不知怎的,它变大了。我怎么处理这个?

预先谢谢。

JPanel CheckPanel = new JPanel();
CheckPanel.setBackground(Color.WHITE);
GridBagConstraints gbc_CheckPanel = new GridBagConstraints();
gbc_CheckPanel.gridwidth = 2;
gbc_CheckPanel.fill = GridBagConstraints.BOTH;
gbc_CheckPanel.gridx = 0;
gbc_CheckPanel.gridy = 1;
CenterPanel.add(CheckPanel, gbc_CheckPanel);
CheckPanel.setLayout(new GridLayout(0, 6, 0, 0));

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:0)

约束

gbc_CheckPanel.fill = GridBagConstraints.BOTH;

告诉GridBagLayout让这个组件使用任何可用的额外空间(也取决于它的首选和最大大小)。使用GridBagConstraints.NONEGridBagConstraints.VERTICAL可以解决此问题,但您仍需确定哪个组件将获得额外空间。

编辑:根据评论,仍然不清楚如何插入其他组件,以及如何指定额外空间的分布。在此示例中,weighty字段用于表示表应接收所有额外空间。也许有助于确定与原始计划的差异:

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class CheckboxConstraints
{
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI()
    {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel centerPanel = new JPanel(new GridBagLayout());
        centerPanel.setBackground(Color.RED);

        fill(centerPanel);
        f.getContentPane().setLayout(new GridLayout(1,1));
        f.getContentPane().add(centerPanel);

        f.setSize(500, 300);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    private static void fill(JPanel centerPanel)
    {
        addSearchBar(centerPanel);
        addCheckBoxPanel(centerPanel);
        addTable(centerPanel);
    }


    private static void addSearchBar(JPanel centerPanel)
    {
        GridBagConstraints gbc = null;
        gbc = new GridBagConstraints();
        gbc.gridwidth = 1;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 0;
        gbc.weighty = 0;
        centerPanel.add(new JButton("Search"), gbc);

        gbc = new GridBagConstraints();
        gbc.gridwidth = 1;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.weightx = 1;
        gbc.weighty = 0;
        centerPanel.add(new JTextField(), gbc);
    }

    private static void addCheckBoxPanel(JPanel centerPanel)
    {
        JPanel checkPanel = new JPanel();
        checkPanel.setBackground(Color.WHITE);
        GridBagConstraints gbc_CheckPanel = new GridBagConstraints();
        gbc_CheckPanel.gridwidth = 2;
        gbc_CheckPanel.fill = GridBagConstraints.BOTH;
        gbc_CheckPanel.gridx = 0;
        gbc_CheckPanel.gridy = 1;
        gbc_CheckPanel.weighty = 0;
        centerPanel.add(checkPanel, gbc_CheckPanel);
        checkPanel.setLayout(new GridLayout(0, 6, 0, 0));

        checkPanel.add(new JCheckBox("C0"));
        checkPanel.add(new JCheckBox("C1"));
        checkPanel.add(new JCheckBox("C2"));
        checkPanel.add(new JCheckBox("C3"));
        checkPanel.add(new JCheckBox("C4"));
        checkPanel.add(new JCheckBox("C5"));
    }

    private static void addTable(JPanel centerPanel)
    {
        JTable table = new JTable(new Object[][] {
            {"C00", "C01" },
            {"C00", "C01" },
        }, new Object[]{ "H0", "H1" });
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridwidth = 2;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.weighty = 1.0;
        JScrollPane scrollPane = new JScrollPane(table);
        centerPanel.add(scrollPane, gbc);
    }

}

答案 1 :(得分:0)

请尝试:

gbc_CheckPanel.fill =GridBagConstraints.HORIZONTAL

并垂直调整空间

gbc_CheckPanel.ipady = 20;  //Not sure this works in your case but this should fix the resizing issue

另一个解决方案是添加一个带有BorderLayout的新JPanel,并使用约束BorderLayout.NORTH和带约束BorderLayout.CENTRE的表

插入带有JCheckBoxes的面板