Java/Swing:尝试让 BorderLayout 与 GridBagLayout 配合良好

发布于 2024-09-14 11:28:07 字数 2256 浏览 2 评论 0原文

我想要一个有 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 我可以处理它,但这不起作用。

I'd like to have a window that has 3 menus, one tied to the left, another tied to the center and the last one tied to the right. Like this:

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

The problem I'm having is that this is the result I get:

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

Here's some sample code (compiles and shows the problem):

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

How can I fix it? I thought that by setting the anchor in the GridBagConstraints I'd take care of it, but that didn't work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

滴情不沾 2024-09-21 11:28:07

您忘记添加:

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

它可以工作。

You forgot to add :

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

Your changed code should look like :

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

It works.

乱了心跳 2024-09-21 11:28:07

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

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

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

If your toolbar looks like a BorderLayout (WEST, CENTER, EAST), why not use a BorderLayout instead of a GridBagLayout?

Anyway, if you insist on using GridBagLayout, set the weightx constraint for toolbar2 to 1. This tells the layout manager that, if more room is available, it should give it all to toolbar2.

  gbc.weightx = 1;
  upper.add(toolbar2, gbc);
  gbc.weightx = 0;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文