仅使用一列时使组件在 GridBagLayout 中跨越多行

Make a component span multiple rows in GridBagLayout when using only one column

下面,你可以看到我尝试绘制泰国国旗的简单示例:

.

这面旗帜有一个特殊的功能。蓝线比其他线大两倍:

public class GridBagLayoutTest {

private static final Insets insets = new Insets(0, 0, 0, 0);


public static void main(String args[]) {

    final JFrame frame = new JFrame("Make a component span multiple rows in GridBagLayout when using only one column");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new GridBagLayout());

    JPanel row1 = new JPanel();
    row1.setBackground(new Color(237, 27, 36));

    JPanel row2 = new JPanel();
    row2.setBackground(Color.WHITE);

    JPanel row3 = new JPanel();
    row3.setBackground(new Color(26, 29, 80));

    JPanel row4 = new JPanel();
    row4.setBackground(Color.WHITE);

    JPanel row5 = new JPanel();
    row5.setBackground(new Color(237, 27, 36));

    //We are trying to draw Flag of Thailand
    addComponent(frame, row1, 0, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
    addComponent(frame, row2, 0, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
    addComponent(frame, row3, 0, 2, 1, 2, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
    addComponent(frame, row4, 0, 4, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
    addComponent(frame, row5, 0, 5, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);


    frame.setSize(500, 200);
    frame.setVisible(true); 

}


private static void addComponent(Container container, Component component, int gridx, int gridy,
  int gridwidth, int gridheight, int anchor, int fill) {
    GridBagConstraints gbc = new GridBagConstraints(gridx, gridy, gridwidth, gridheight, 1.0, 1.0,
                                                    anchor, fill, insets, 0, 0);
    container.add(component, gbc);
}


}

我不想使用 GridBagConstraint 的 属性 ipady 因为我想在以下情况下保持相同的行为我调整框架的大小。 第三行(蓝色)的高度必须始终是其他行的两倍。

我正在使用 属性 gridheight。对于蓝线,我将值 2 赋给了这个 属性,但是没有任何反应。

你有实现这个目标的想法吗?

给你。

当您使用 GridBagLayout 时,您对 Swing 组件大小的唯一控制是使用 weightx 和 weighty 参数。

我对您的代码做了一些更改,主要是为了排除其他问题。

这是代码。

package com.ggl.testing;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class GridBagLayoutTest implements Runnable {

    private static final Insets insets = new Insets(0, 0, 0, 0);

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new GridBagLayoutTest());
    }

    @Override
    public void run() {
        JFrame frame = new JFrame(
                "Make a component span multiple rows in GridBagLayout when using only one column");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(createFlag());
        frame.pack();
        frame.setVisible(true);

    }

    private JPanel createFlag() {
        JPanel panel = new JPanel();
        panel.setLayout(new GridBagLayout());
        panel.setPreferredSize(new Dimension(450, 300));

        JPanel row1 = new JPanel();
        row1.setBackground(new Color(237, 27, 36));

        JPanel row2 = new JPanel();
        row2.setBackground(Color.WHITE);

        JPanel row3 = new JPanel();
        row3.setBackground(new Color(26, 29, 80));

        JPanel row4 = new JPanel();
        row4.setBackground(Color.WHITE);

        JPanel row5 = new JPanel();
        row5.setBackground(new Color(237, 27, 36));

        // We are trying to draw Flag of Thailand
        int gridy = 0;
        addComponent(panel, row1, 0, gridy++, 1, 1, 1D, 1D,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH);
        addComponent(panel, row2, 0, gridy++, 1, 1, 1D, 1D,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH);
        addComponent(panel, row3, 0, gridy++, 1, 1, 1D, 2D,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH);
        addComponent(panel, row4, 0, gridy++, 1, 1, 1D, 1D,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH);
        addComponent(panel, row5, 0, gridy++, 1, 1, 1D, 1D,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH);

        return panel;
    }

    private void addComponent(Container container, Component component,
            int gridx, int gridy, int gridwidth, int gridheight,
            double weightx, double weighty, int anchor, int fill) {
        GridBagConstraints gbc = new GridBagConstraints(gridx, gridy,
                gridwidth, gridheight, weightx, weighty, anchor, fill, insets,
                0, 0);
        container.add(component, gbc);
    }

}