仅使用一列时,使组件跨GridBagLayout中的多行

时间:2016-03-07 12:54:25

标签: java swing gridbaglayout

下面,你可以看到一个简单的例子,我试图绘制泰国的旗帜:

image flag Thailand

此标志有一个特殊功能。蓝线比其他线大两倍:

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设置为此属性,但没有任何反应。

你有想法达到这个目标吗?

1 个答案:

答案 0 :(得分:4)

你走了。

Flag

当你使用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);
    }

}