GridBagLayout

时间:2015-09-30 08:55:52

标签: java swing layout-manager gridbaglayout

我在这段代码中发现了最近奇怪的事情:

import java.awt.Color;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.io.IOException;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


public class Test {

    public static void main(String [] args) throws IOException {
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        panel.setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();

        gbc.insets.top = 5;
        gbc.insets.right = 5;

        add(0, 0, 1, 1, 2, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, gbc, panel, createPanel());
        add(2, 0, 1, 1, 2, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, gbc, panel, createPanel());
        add(0, 1, 1, 1, 2, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, gbc, panel, createPanel());
        add(2, 1, 1, 1, 2, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, gbc, panel, createPanel());
        add(0, 2, 1, 1, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, gbc, panel, createPanel());
        add(1, 2, 1, 1, 2, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, gbc, panel, createPanel());
        add(3, 2, 1, 1, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, gbc, panel, createPanel());
        add(0, 3, 1, 1, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, gbc, panel, createPanel());
        add(1, 3, 1, 1, 2, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, gbc, panel, createPanel());
        add(3, 3, 1, 1, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, gbc, panel, createPanel());
        add(0, 4, 1, 1, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, gbc, panel, createPanel());
        add(1, 4, 1, 1, 2, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, gbc, panel, createPanel());
        add(3, 4, 1, 1, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, gbc, panel, createPanel());
        add(0, 5, 1, 1, 4, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, gbc, panel, createPanel());

        frame.setContentPane(panel);

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    private static void add(int gridx, int gridy, int weightx, int weighty, int gridw, int gridh, int fill, int anchor, GridBagConstraints gbc, Container container, JComponent component) {
        gbc.gridx = gridx;
        gbc.gridy = gridy;
        gbc.weightx = weightx;
        gbc.weighty = weighty;
        gbc.gridwidth = gridw;
        gbc.gridheight = gridh;
        gbc.fill = fill;
        gbc.anchor = anchor;

        component.add(Box.createHorizontalStrut(5));
        component.add(new JLabel("(" + gridx + "; " + gridy + ")"));
        component.add(Box.createHorizontalStrut(5));
        component.add(new JLabel("gridwidth = " + gridw));
        component.add(Box.createHorizontalStrut(5));

        container.add(component, gbc);
    }

    private static JPanel createPanel() {
        JPanel panel1 = new JPanel();
        panel1.setBackground(Color.WHITE);
        panel1.setBorder(BorderFactory.createLineBorder(Color.RED));

        return panel1;
    }
}

它在运行中的表现如何:

java_weird

每个面板的括号和网格宽度都有坐标。从最后一行可以看出,全局网格宽度为4.第1列(x = 0)中的第3-5行(y = 2,y = 3,y = 4)的网格宽度等于1.但在前两行中第一列的网格宽度等于2,它应该在帧的中间划分,但我们不是。为什么呢?

1 个答案:

答案 0 :(得分:1)

我自己找到了什么:

public class Test {

    public static void main(String [] args) throws IOException {
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

        GridBagConstraints gbc = new GridBagConstraints();

        gbc.insets.top = 5;
        gbc.insets.right = 5;

        JPanel topPanel = new JPanel();
        topPanel.setLayout(new GridBagLayout());
        JPanel bottomPanel = new JPanel();
        bottomPanel.setLayout(new GridBagLayout());

        add(0, 0, 1, 1, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, gbc, topPanel, createPanel());
        add(1, 0, 1, 1, 2, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, gbc, topPanel, createPanel());
        add(0, 0, 1, 1, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, gbc, bottomPanel, createPanel());
        add(1, 0, 1, 1, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, gbc, bottomPanel, createPanel());
        add(2, 0, 1, 1, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, gbc, bottomPanel, createPanel());
        add(0, 2, 1, 1, 3, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, gbc, bottomPanel, createPanel());

        panel.add(topPanel);
        panel.add(bottomPanel);

        frame.setContentPane(panel);

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    private static void add(int gridx, int gridy, int weightx, int weighty, int gridw, int gridh, int fill, int anchor, GridBagConstraints gbc, Container container, JComponent component) {
        gbc.gridx = gridx;
        gbc.gridy = gridy;
        gbc.weightx = weightx;
        gbc.weighty = weighty;
        gbc.gridwidth = gridw;
        gbc.gridheight = gridh;
        gbc.fill = fill;
        gbc.anchor = anchor;

        component.add(Box.createHorizontalStrut(5));
        component.add(new JLabel("(" + gridx + "; " + gridy + ")"));
        component.add(Box.createHorizontalStrut(5));
        component.add(new JLabel("gridwidth = " + gridw));
        component.add(Box.createHorizontalStrut(5));

        container.add(component, gbc);
    }

    private static JPanel createPanel() {
        JPanel panel1 = new JPanel();
        panel1.setBackground(Color.WHITE);
        panel1.setBorder(BorderFactory.createLineBorder(Color.RED));

        return panel1;
    }
}

结果如下:

java weird

我做过的事情:将内容窗格划分为两个单独的面板 - 顶部(单行具有两个等分的单元格)和底部(两行:一行有3个单元,另一行有1个单宽)细胞)。内容窗格现在具有BoxLayout而不是GBL,并且它按预期工作。但是我对GridBagLayout很失望,这对我来说是完美的布局。