简体   繁体   English

GridBagLayout-如何使此单元格填充其下方的空间?

[英]GridBagLayout - How can I make this cell fill the space below it?

I'm creating a JFrame with 4 JPanels. 我正在用4个JPanels创建一个JFrame。 Using GridBagLayout, I create three rows and two columns, two panels per column. 使用GridBagLayout,我创建了三行两列,每列两个面板。 By changing the blue panel's cellheight from 1 to 2 I can make it cover the cell below it. 通过将蓝色面板的单元格高度从1更改为2,我可以使其覆盖其下方的单元格。 I'd like to do the same for the green panel, to fill the space below it. 我想对绿色面板执行相同的操作,以填充其下方的空间。 Here's what my code currently produces: 这是我的代码当前产生的内容:

在此处输入图片说明

I tried changing the green panel's gridheight to 2 but I end up with this: 我尝试将绿色面板的gridheight更改为2,但最终得到了以下结果:

在此处输入图片说明

Am I using GridBagLayout incorrectly? 我是否使用GridBagLayout不正确? What is the appropriate way of doing this? 这样做的适当方法是什么?

Here's my code: 这是我的代码:

import java.awt.Color;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main {

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        addComponents(frame.getContentPane());
        frame.setSize(800, 600);
        frame.setVisible(true);
    }

    public static void addComponents(Container contentPane) {
        contentPane.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.insets = new Insets(3,3,3,3);
        c.fill = GridBagConstraints.BOTH;

        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 1;
        c.gridheight = 2;
        c.weightx = 1;
        c.weighty = 0.85;
        JPanel panel1 = new JPanel();
        panel1.setBackground(Color.BLUE);
        contentPane.add(panel1, c);

        c.gridx = 0;
        c.gridy = 2;
        c.gridwidth = 1;
        c.gridheight = 1;
        c.weightx = 1.1;
        c.weighty = 0.35;
        JPanel panel2 = new JPanel();
        panel2.setBackground(Color.YELLOW);
        contentPane.add(panel2, c);

        c.gridx = 1;
        c.gridy = 0;
        c.gridwidth = 1;
        c.gridheight = 1;
        c.weightx = 0.15;
        c.weighty = 0.5;
        JPanel panel3 = new JPanel();
        panel3.setBackground(Color.RED);
        contentPane.add(panel3, c);

        c.gridx = 1;
        c.gridy = 1;
        c.gridwidth = 1;
        c.gridheight = 1;
        c.weightx = 0.38;
        c.weighty = 0.5;
        JPanel panel4 = new JPanel();
        panel4.setBackground(Color.GREEN);
        contentPane.add(panel4, c);
    }
}

I create three rows and two columns, 我创建三行两列,

Actually you only have two rows, since you only ever add components with a gridy value of 0 and 1. The fact that one of the components has a gridheight of 2 doesn't create a new row. 实际上,您只有两行,因为您只添加了网格值为0和1的组件。组件之一的网格高度为2的事实不会创建新行。

One solution is to use nested panels. 一种解决方案是使用嵌套面板。

Create a panel for the blue and yellow components. 为蓝色和黄色组件创建一个面板。 This would use a GridBagLayout with one column and 2 rows. 这将使用具有一列和两行的GridBagLayout。 You then set the weighty values for each component to give your desired height. 然后,为每个组件设置权重值以提供所需的高度。

Then you create a second panel for the red and green components again with 1 column and two rows. 然后,再次用1列和2行为红色和绿色组件创建第二个面板。 The weighty would be set to 0.5 for each. 每个的权重将设置为0.5。

Finally you create a third panel with 2 columns and one row. 最后,创建第三个面板,该面板具有2列和1行。 You set the desired weightx and add the above two panels to this panel and then add the panel to the frame. 您设置所需的权重x,并将以上两个面板添加到该面板,然后将该面板添加到框架。

The layout starts to behave as expected if there is a 3rd column of three cells, each with a row span of 1. 如果有三个单元格的第三列,每个单元格的行跨度为1,则布局开始按预期方式运行。

在此处输入图片说明 在此处输入图片说明

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

public class AnotherFourPanelLayout {

    private JComponent ui = null;

    AnotherFourPanelLayout() {
        initUI();
    }

    public void initUI() {
        if (ui != null) {
            return;
        }

        ui = new JPanel(new BorderLayout(4, 4));
        ui.setBorder(new EmptyBorder(4, 4, 4, 4));

        addComponents(ui);
    }

    public void addComponents(Container contentPane) {
        contentPane.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.insets = new Insets(3, 3, 3, 3);
        c.fill = GridBagConstraints.BOTH;

        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 1;
        c.gridheight = 2;
        c.weightx = 0.66;
        c.weighty = 0.66;
        JPanel panel1 = new JPanel();
        addLabel(panel1, c);
        panel1.setBackground(Color.CYAN);
        contentPane.add(panel1, c);

        c.gridx = 0;
        c.gridy = 2;
        c.gridwidth = 1;
        c.gridheight = 1;
        //c.weightx = 1.1; // logical?
        c.weightx = 0.66; 
        c.weighty = 0.33;
        JPanel panel2 = new JPanel();
        addLabel(panel2, c);
        panel2.setBackground(Color.YELLOW);
        contentPane.add(panel2, c);

        c.gridx = 1;
        c.gridy = 0;
        c.gridwidth = 1;
        c.gridheight = 1;
        c.weightx = 0.33;
        c.weighty = 0.33;
        JPanel panel3 = new JPanel();
        addLabel(panel3, c);
        panel3.setBackground(Color.RED);
        contentPane.add(panel3, c);

        c.gridx = 1;
        c.gridy = 1;
        c.gridwidth = 1;
        c.gridheight = 2;
        c.weightx = 0.33;
        c.weighty = 0.66;
        JPanel panel4 = new JPanel();
        addLabel(panel4, c);
        panel4.setBackground(Color.GREEN);
        contentPane.add(panel4, c);

        // hack to fix?
        c.gridx = 2;
        c.gridy = 0;
        c.gridwidth = 1;
        c.gridheight = 1;
        c.weightx = 0.01;
        c.weighty = 0.33;
        JPanel panelH1 = new JPanel();
        //addLabel(panelH1, c);
        panelH1.setBackground(Color.MAGENTA);
        contentPane.add(panelH1, c);

        c.gridy = 1;
        JPanel panelH2 = new JPanel();
        //addLabel(panelH2, c);
        panelH2.setBackground(Color.MAGENTA);
        contentPane.add(panelH2, c);

        c.gridy = 2;
        JPanel panelH3 = new JPanel();
        //addLabel(panelH3, c);
        panelH3.setBackground(Color.MAGENTA);
        contentPane.add(panelH3, c);
    }

    private void addLabel(JPanel panel, GridBagConstraints gbc) {
        panel.add(new JLabel(constraintsToString(gbc)));
    }

    private String constraintsToString(GridBagConstraints gbc) {
        StringBuilder sb = new StringBuilder();

        sb.append("<html><table>");
        sb.append(addRowToTable("Grid X", gbc.gridx));
        sb.append(addRowToTable("Grid Y", gbc.gridy));
        sb.append(addRowToTable("Weight X", gbc.weightx));
        sb.append(addRowToTable("Weight Y", gbc.weighty));
        sb.append(addRowToTable("Grid Width", gbc.gridwidth));
        sb.append(addRowToTable("Grid Height", gbc.gridheight));

        return sb.toString();
    }

    private String addRowToTable(String label, double value) {
        StringBuilder sb = new StringBuilder("<tr><td>");

        sb.append(label);
        sb.append("</td><td>");
        sb.append(value);
        sb.append("</td></tr>");


        return sb.toString();
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                AnotherFourPanelLayout o = new AnotherFourPanelLayout();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM