Java / Swing:试图让BorderLayout与GridBagLayout一起玩得很好

时间:2010-08-18 05:31:46

标签: java user-interface swing gridbaglayout

我想要一个有3个菜单的窗口,一个绑在左边,另一个绑在中间,最后一个绑在右边。像这样:

--------------------------------------------
-toolbar1---------toolbar2---------toolbar3-
--------------------------------------------
-                                          -
-  rest of the window does something here  -

我遇到的问题是这是我得到的结果:

--------------------------------------------
---------toolbar1toolbar2toolbar3-----------
--------------------------------------------
-                                          -
-  rest of the window does something here  -

以下是一些示例代码(编译并显示问题):

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class TestClass extends JFrame {

    public TestClass() {
        super("test");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        final JPanel upper = new JPanel();
        upper.setLayout(new GridBagLayout());
        final GridBagConstraints gbc = new GridBagConstraints();

        final JButton toolbar1 = new JButton("toolbar1");
        final JButton toolbar2 = new JButton("toolbar2");
        final JButton toolbar3 = new JButton("toolbar3");

        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.WEST;
        upper.add(toolbar1, gbc);

        gbc.gridx = 1;
        gbc.anchor = GridBagConstraints.CENTER;
        upper.add(toolbar2, gbc);

        gbc.gridx = 2;
        gbc.anchor = GridBagConstraints.EAST;
        upper.add(toolbar3, gbc);

        add(upper, BorderLayout.NORTH);

        final JPanel something = new JPanel();
        something.setBackground(Color.WHITE);
        something.setPreferredSize(new Dimension(600, 600));
        something.repaint();
        add(something, BorderLayout.CENTER);

        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public static void main(String[] args) {
        final TestClass test = new TestClass();
    }
}

我该如何解决?我认为通过在GridBagConstraints中设置anchor我会照顾它,但这不起作用。

2 个答案:

答案 0 :(得分:1)

如果您的工具栏看起来像BorderLayout(WEST,CENTER,EAST),为什么不使用BorderLayout而不是GridBagLayout?

无论如何,如果你坚持使用GridBagLayout,请将toolbar2的weightx约束设置为1.这告诉布局管理器,如果有更多空间可用,它应该全部给予toolbar2。

  gbc.weightx = 1;
  upper.add(toolbar2, gbc);
  gbc.weightx = 0;

答案 1 :(得分:1)

你忘了添加:

gbc.weightx = 1.0;
gbc.weighty = 1.0;

您更改的代码应如下所示:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class TestClass extends JFrame {

    public TestClass() {
        super("test");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        final JPanel upper = new JPanel();
        GridBagLayout gridbag = new GridBagLayout();
        upper.setLayout(gridbag);
        GridBagConstraints gbc = new GridBagConstraints();

        final JButton toolbar1 = new JButton("toolbar1");
        final JButton toolbar2 = new JButton("toolbar2");
        final JButton toolbar3 = new JButton("toolbar3");

        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        gbc.anchor = GridBagConstraints.WEST;
        upper.add(toolbar1, gbc);


        gbc.gridx = 1;
        gbc.anchor = GridBagConstraints.CENTER;
        upper.add(toolbar2, gbc);


        gbc.gridx = 2;
        gbc.anchor = GridBagConstraints.EAST;
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        upper.add(toolbar3, gbc);


        add(upper, BorderLayout.NORTH);

        final JPanel something = new JPanel();
        something.setBackground(Color.WHITE);
        something.setPreferredSize(new Dimension(600, 600));
        something.repaint();
        add(something, BorderLayout.CENTER);

        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public static void main(String[] args) {
        final TestClass test = new TestClass();
    }
}

有效。