Java GridBagLayout和GridBagConstraints

时间:2015-01-22 08:45:36

标签: java

我发现很多帖子谈论如何使用GridBagLayout,但它们总是与我需要的相反。

例如,在this post中,他们的顶行按钮跨越宽度,而下面只有一个按钮。我的兴趣主要是前两行。

enter image description here

但是考虑到反过来,我想在第1行中只有1个按钮来跨越第2行中正好4个按钮的宽度以及从那里开始的任何倍数。

[=========按钮======] [=========按钮======]

[按钮] [按钮] [按钮] [按钮] [按钮] [按钮] [按钮] [按钮]

1 个答案:

答案 0 :(得分:1)

以下是如何实现您正在寻找的布局。

public GridBagConstraintsFrame() {
    Container c = getContentPane();
    JPanel p = new JPanel(new GridBagLayout());
    c.setLayout(new BorderLayout());

    {
        JButton button1 = new JButton("Button 1");
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridheight = 1;
        gbc.gridwidth = 4;
        gbc.weightx = 0;
        gbc.weighty = 0;
        gbc.fill = gbc.HORIZONTAL;
        p.add(button1, gbc);
    }

    {
        JButton button2 = new JButton("Button 2");
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 4;
        gbc.gridy = 0;
        gbc.gridheight = 1;
        gbc.gridwidth = 4;
        gbc.weightx = 0;
        gbc.weighty = 0;
        gbc.fill = gbc.HORIZONTAL;
        p.add(button2, gbc);
    }

    {
        JButton button3 = new JButton("Button 3");
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridheight = 1;
        gbc.gridwidth = 1;
        gbc.weightx = 0;
        gbc.weighty = 0;
        p.add(button3, gbc);
    }

    {
        JButton button3 = new JButton("Button 4");
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.gridheight = 1;
        gbc.gridwidth = 1;
        gbc.weightx = 0;
        gbc.weighty = 0;
        p.add(button3, gbc);
    }

    {
        JButton button3 = new JButton("Button 5");
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 2;
        gbc.gridy = 1;
        gbc.gridheight = 1;
        gbc.gridwidth = 1;
        gbc.weightx = 0;
        gbc.weighty = 0;
        p.add(button3, gbc);
    }

    {
        JButton button3 = new JButton("Button 6");
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 3;
        gbc.gridy = 1;
        gbc.gridheight = 1;
        gbc.gridwidth = 1;
        gbc.weightx = 0;
        gbc.weighty = 0;
        p.add(button3, gbc);
    }

    {
        JButton button3 = new JButton("Button 7");
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 4;
        gbc.gridy = 1;
        gbc.gridheight = 1;
        gbc.gridwidth = 1;
        gbc.weightx = 0;
        gbc.weighty = 0;
        p.add(button3, gbc);
    }

    {
        JButton button3 = new JButton("Button 8");
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 5;
        gbc.gridy = 1;
        gbc.gridheight = 1;
        gbc.gridwidth = 1;
        gbc.weightx = 0;
        gbc.weighty = 0;
        p.add(button3, gbc);
    }

    {
        JButton button3 = new JButton("Button 9");
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 6;
        gbc.gridy = 1;
        gbc.gridheight = 1;
        gbc.gridwidth = 1;
        gbc.weightx = 0;
        gbc.weighty = 0;
        p.add(button3, gbc);
    }

    {
        JButton button3 = new JButton("Button 10");
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 7;
        gbc.gridy = 1;
        gbc.gridheight = 1;
        gbc.gridwidth = 1;
        gbc.weightx = 0;
        gbc.weighty = 0;
        p.add(button3, gbc);
    }

    c.add(p, BorderLayout.CENTER);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setTitle("Gridbag constraints.");
    pack();
    setVisible(true);
}

这是它的外观。 enter image description here

如果您想要解释,请在评论中告诉我,我会尝试解释每一段代码。

PS。这是一个JFrame。