调整 JFrame 大小时去除组件之间的额外空间

Getting rid of extra spaces between components when the JFrame is resized

我正在使用 GridBagLayout 在框架中排列一些组件。
首次创建框架时,组件之间有一个不错的 space。

但是一旦我调整框架的大小,组件之间就会有很多不需要的 space

我尝试按照一些用户的建议调整权重和插图,但似乎并没有解决问题

import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JTextArea;

public class Frame1 extends JFrame {

    JLabel one = new JLabel("one");
    JLabel two = new JLabel("two");
    JLabel three = new JLabel("three");

    JTextField oneF = new JTextField(20);
    JTextField twoF = new JTextField(20);
    JTextField threeF = new JTextField(20);

    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("menu");

    GridBagConstraints c = new GridBagConstraints();

    public Frame1() {
        setTitle("GridBagLayout Test");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new GridBagLayout());

        menuBar.add(menu);

        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = c.REMAINDER;
        c.fill = c.HORIZONTAL;
        c.gridheight = 1;
        c.weightx = 1;
        c.weighty = 1;
        c.anchor = c.NORTH;
        c.insets = new Insets(5,5,5,5);
        add(menuBar, c);

        c.gridx = 0;
        c.gridy = 1;
        c.gridwidth = 1;
        c.fill = c.NONE;
        c.gridheight = 1;
        c.weightx = 1;
        c.weighty = 1;
        c.anchor = c.NORTH;
        c.insets = new Insets(5,5,5,5);
        add(one, c);

        c.gridx = 1;
        c.gridy = 1;
        c.gridwidth = 1;
        c.fill = c.NONE;
        c.gridheight = 1;
        c.weightx = 1;
        c.weighty = 1;
        c.anchor = c.NORTH;
        c.insets = new Insets(5,5,5,5);
        add(oneF, c);

        c.gridx = 0;
        c.gridy = 2;
        c.gridwidth = 1;
        c.fill = c.NONE;
        c.gridheight = 1;
        c.weightx = 1;
        c.weighty = 1;
        c.anchor = c.NORTH;
        c.insets = new Insets(5,5,5,5);
        add(two, c);

        c.gridx = 1;
        c.gridy = 2;
        c.gridwidth = 1;
        c.fill = c.NONE;
        c.gridheight = 1;
        c.weightx = 1;
        c.weighty = 1;
        c.anchor = c.NORTH;
        c.insets = new Insets(5,5,5,5);
        add(twoF, c);

        c.gridx = 0;
        c.gridy = 3;
        c.gridwidth = 1;
        c.fill = c.NONE;
        c.gridheight = 1;
        c.weightx = 1;
        c.weighty = 1;
        c.anchor = c.NORTH;
        c.insets = new Insets(5,5,5,5);
        add(three, c);

        c.gridx = 1;
        c.gridy = 3;
        c.gridwidth = 1;
        c.fill = c.NONE;
        c.gridheight = 1;
        c.weightx = 1;
        c.weighty = 1;
        c.anchor = c.NORTH;
        c.insets = new Insets(5,5,5,5);
        add(threeF, c);

       //setResizable(false);
       pack();
       setVisible(true);
    }

}

ps:- 我是 GUI 编程的新手,所以请原谅我的任何菜鸟错误。

edit 1:这是我完成后想要的。我知道目前它看起来并不接近我的想法......我仍在努力

谢谢

我不知道它应该是什么样子,但你可以尝试为标签设置 c.anchor=GridBagConstraints.EAST 和为文本字段设置 c.anchor=GridBagConstraints.WEST

也尝试设置 c.fill = GridBadConstraints.BOTH

我们的想法是添加空行/列,这些空行/列将增长以填充可用 space:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JTextField;

public class Frame1 extends JFrame {

    JLabel one = new JLabel("one");
    JLabel two = new JLabel("two");
    JLabel three = new JLabel("three");

    JTextField oneF = new JTextField(20);
    JTextField twoF = new JTextField(20);
    JTextField threeF = new JTextField(20);

    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("menu");

    public Frame1() {

        setTitle("GridBagLayout Test");
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        GridBagLayout gridBagLayout = new GridBagLayout();
        gridBagLayout.rowHeights = new int[]{0, 0, 0, 0}; //this defines 4 rows
        //make 2 last empty row grow 
        gridBagLayout.rowWeights = new double[]{0.0, 0.0, 1.0,1.0};

        //do the same for columns 
        gridBagLayout.columnWidths = new int[]{0, 0, 0, 0};
        gridBagLayout.columnWeights = new double[]{0.0, 0.0, 1.0,1.0};

        getContentPane().setLayout(gridBagLayout);

        menuBar.add(menu);

        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 5;
        c.fill = c.HORIZONTAL;
        c.anchor = c.NORTH;
        c.insets = new Insets(5, 5, 5, 0);
        getContentPane().add(menuBar, c);

        //better have a new GridBagConstraints for each component added 
        GridBagConstraints c1 = new GridBagConstraints();
        c1.gridx = 0;
        c1.gridy = 1;
        c1.gridwidth = 1;
        c1.fill = c1.NONE;
        c1.anchor = c1.NORTH;
        c1.insets = new Insets(5, 5, 0, 5);
        getContentPane().add(one, c1);

        GridBagConstraints c2 = new GridBagConstraints();
        c2.gridx = 1;
        c2.gridy = 1;
        c2.fill = c2.NONE;
        c2.anchor = GridBagConstraints.NORTHWEST;
        c2.insets = new Insets(5, 5, 0, 5);
        getContentPane().add(oneF, c2);

        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        new Frame1();
    }
}

编辑: 回应您的编辑:为 "cover art"

使用额外的 "growing" 列
  • 使用嵌套布局(几个 LayoutManager 的组合),你的图片告诉我,

  • 您仍然可以对放置在左侧的组件使用 GridBagLayout

  • 在我的代码中(尽可能简单的想法)JComponent 放置在左侧无法调整大小,因为受到 LayoutManager 的默认设置的限制,更多内容在 Oracle tutorial

.

.

画自SSCCE/MCVE

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MyFrame {

    private static final long serialVersionUID = 1L;
    private JFrame myFrame = new JFrame("Whatever");
    private JPanel parentPanel = new JPanel(new BorderLayout(10, 10)) {

        private static final long serialVersionUID = 1L;

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(600, 400);
        }

        @Override
        public Color getBackground() {
            return new Color(255, 000, 000);
        }
    };
    private JPanel leftPanel = new JPanel(/*default is FlowLayout*/) {

                private static final long serialVersionUID = 1L;

                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(300, 400);
                }

                @Override
                public Color getBackground() {
                    return new Color(255, 255, 000);
                }
            };
    private JPanel leftChildPanel = new JPanel() {

        private static final long serialVersionUID = 1L;

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(300, 400);
        }

        @Override
        public Color getBackground() {
            return new Color(255, 255, 225);
        }
    };
    private JPanel rightPanel = new JPanel(new BorderLayout(10, 10)) {

        private static final long serialVersionUID = 1L;

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(300, 380);
        }

        @Override
        public Color getBackground() {
            return new Color(000, 255, 225);
        }
    };

    public MyFrame() {
        parentPanel.add(leftPanel, BorderLayout.WEST);
        leftPanel.add(leftChildPanel);
        parentPanel.add(rightPanel);
        myFrame.add(parentPanel);
        myFrame.setLocation(150, 150);
        myFrame.pack();
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myFrame.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            new MyFrame();
        });
    }
}

问题是您对 c.weightxc.weighty 的分配。 weightx 和 weighty 确定当容器比容纳组件的首选大小时所需的大时,如何将额外 space 分配给 GridBagLayout 中的网格单元。

所有单元格的 weightx 和 weighty 都应该为零,除了那些你想在 window 变大时变大的单元格。