Java/Swing : Trying to get BorderLayout to play nice with GridBagLayout

标签 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 我会处理它,但这没有用。

最佳答案

你忘了添加:

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();
    }
}

有效。

关于Java/Swing : Trying to get BorderLayout to play nice with GridBagLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3509211/

相关文章:

java - 保密信息的 secret

javascript - 设计 Metro 风格网站的 CSS 和 JS 技巧

java - 适当事件后图形不显示

Java SWT : How to set the window to be resizable\not resizable on the fly?

java - 在java中运行没有线程的计时器

c++ - PostMessage() 成功但我的消息处理代码从未收到消息

java - 刷新jTable

java - 从 JToolbar 中拖动 JFrame

eclipse - 如何使用 maven 创建一个 Swing 应用程序?

java - 如何提高Java中的select查询性能?