有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用GridBagLayout维护jpanel的维度大小?

这是我的密码。当我在其他布局中调整程序的大小时,除了CheckPanel(使用GridBagLayout)之外,它工作正常;不知怎的,它变得更大了。我该怎么办

提前谢谢

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


共 (1) 个答案

  1. # 1 楼答案

    约束

    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);
        }
    
    }