【问题标题】:Grid bag layout not displaying the way I want网格包布局未按我想要的方式显示
【发布时间】:2012-06-11 08:58:40
【问题描述】:

所以我有 6 个面板,所有面板都使用网格布局。 我用gridbaglayout把它们放在一起,这是我想要的设计

结果一团糟 “第二个”面板变得离右边更远了 第三个面板向左挤压了很多,这是一场灾难。 这是我的网格包布局代码

    c.gridx = 0; c.gridy = 0;
    add (first,c); 

    c.gridx = 2; //so that the second panel starts from the center and is divided evenly with the first panel
    add(second,c);

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

    c.gridx = 1; 
    add(fourth,c);

    c.gridx = 2;
    add(fifth,c);

    c.gridx = 3;
    add(sixth,c);

感谢任何帮助。

【问题讨论】:

    标签: java swing user-interface layout-manager gridbaglayout


    【解决方案1】:

    在您编写c.gridx = 0 and c.gridy = 0 时,您忘记指定您希望此JPanel 占用多少列。为了指定这一点,您应该使用c.gridwidth = 2,它告诉布局这个JPanel 需要两列的空间。

    要得到这样的输出:

    这是一个小的工作示例:

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

    【讨论】:

    • @trashgod :感谢您的编辑,现在更有意义了。以前,我的英语似乎没有那种意义。当 must 变成 should 时,我才意识到我的英语在解释方面真的不是那么好 :-) 再次感谢 :-)
    【解决方案2】:

    这是(另一种)替代嵌套布局。面板 3 到 6 将获得额外的高度,额外的宽度将平均分配给所有 6 个面板。

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

    【讨论】:

      【解决方案3】:

      我对@9​​87654321@ 不够熟悉,无法快速发现您可以改进代码以解决此问题。但是另一种方法可能是可能的,使用嵌套的BorderLayouts。

      面板“第一”、“第三”和“第四”可以放置在一个JPanelA内,BorderLayout 分别位于NORTHWESTEAST。 JPanel B 中的“第二”、“第五”和“第六”面板也可以这样做。

      然后你将这两个面板(AB)分组到另一个面板中,BorderLayoutWESTEAST

      【讨论】:

      • “然后你将这 2 个面板(AB)分组到另一个面板中,用一个..” GridLayout(1,0) 如果我理解你的 AB 正确,要求它们伸展到可用宽度。
      • 感谢大家的快速响应,但我对使用相同网格袋布局的解决方案有点好奇。
      【解决方案4】:

      添加这个:

      c.gridwidth = 2;

      在添加 FIRST 和 SECOND 面板之前,在添加其他面板之前将 gridwidth 设置回 1。 或者,您可以按照上面的建议将它们进一步分开。您可以为每一行使用两个 FlowLayout,并将这两个添加到另一个具有 NORTH/SOUTH 的 BorderLayout 的面板。

      【讨论】:

      • 编辑:我认为它修复了第一个和第二个面板,谢谢汤姆
      猜你喜欢
      • 2012-04-06
      • 2022-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多