Java Swing GridBagLayout - 在没有 space 的情况下添加按钮

Java Swing GridBagLayout - adding buttons without space

如何消除 gridBagLayout 造成的间距并使它们粘在一起?
这是我的代码,只是添加了 3 个按钮。 我读了这个问题,但没有完整的解决方案 How to fix gap in GridBagLayout;
我只想将所有按钮放在 JFrame 的顶部。

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

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

public class MyProblem {

    private JFrame frame = new JFrame();

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

    public MyProblem() {
        frame.setLayout(new GridBagLayout());
        GridBagConstraints gc = new GridBagConstraints();
        gc.weightx = 1;
        gc.weighty = 1;
        gc.gridx = 0;
        gc.gridy = 0;
        gc.fill = GridBagConstraints.HORIZONTAL;
        gc.anchor = GridBagConstraints.NORTH;
        for (int i = 0; i < 3; i++) {
            JPanel jPanel = new JPanel(new BorderLayout());
            jPanel.setSize(80, 80);
            jPanel.add(new JButton("Button " + i),BorderLayout.PAGE_START);
            frame.add(jPanel, gc);
            gc.gridy++;
        }

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500, 500);
        frame.setVisible(true);
    }

}

我的按钮是什么样子的:

我希望我的按钮看起来像:

布局只包含一列按钮,所以..

变化:

        gc.fill = GridBagConstraints.HORIZONTAL;

收件人:

        gc.fill = GridBagConstraints.BOTH;

编辑 1

I want to keep buttons to the same height as described in picture and just putting them at the top.

使用组合布局可以很容易地将它们限制在顶部。在这种情况下,我们可以将带有按钮的面板添加到带有 BorderLayout 的面板的 PAGE_START 中。边框布局的那部分将拉伸子组件(我们的面板 GridLayout)的 宽度 以填充可用的 space,但它会 尊重其中任何内容的高度 - 仅根据需要为组件提供尽可能多的垂直space。

编辑 2

这是一个实现上述想法的MCVE。外部面板(带有青色边框)用于限制按钮面板(带有橙色边框)的高度。有关其工作原理的更多详细信息,请参阅源代码中的评论。

import java.awt.*;
import javax.swing.*;
import javax.swing.border.LineBorder;

public class MyProblem {

    private JFrame frame = new JFrame();

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

    public MyProblem() {
        frame.setLayout(new BorderLayout()); // actually the default

        // we will use this panel to constrain the panel with buttons
        JPanel ui = new JPanel(new BorderLayout());
        frame.add(ui);
        ui.setBorder(new LineBorder(Color.CYAN, 3));

        // the panel (with GridLayout) for the buttons
        JPanel toolPanel = new JPanel(new GridLayout(0,1,0,4)); // added some gap
        toolPanel.setBorder(new LineBorder(Color.ORANGE, 3));
        // toolPanel will appear at the top of the ui panel
        ui.add(toolPanel, BorderLayout.PAGE_START); 
        for (int i = 0; i < 3; i++) {
            toolPanel.add(new JButton("Button " + i));
        }

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //frame.setSize(500, 500); // instead..
        frame.pack(); // pack will make it as small as it can be.
        frame.setMinimumSize(frame.getSize()); // nice tweak..
        frame.setVisible(true);
    }
}

运行这段代码我想你会从中得到帮助

import java.awt.*;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Think {

public static void addComponentsToPane(Container pane) {

    JButton jbnButton;
    pane.setLayout(new GridBagLayout());
    GridBagConstraints gBC = new GridBagConstraints();
    gBC.fill = GridBagConstraints.HORIZONTAL;

    jbnButton = new JButton("Button 1");
    gBC.weightx = 0.5;
    gBC.gridx = 0;
    gBC.gridy = 0;
    pane.add(jbnButton, gBC);

    jbnButton = new JButton("Button 3");
    gBC.gridx = 2;
    gBC.gridy = 0;
    pane.add(jbnButton, gBC);

    jbnButton = new JButton("Button 4");
    gBC.ipady = 40;     //This component has more breadth compared to other buttons
    gBC.weightx = 0.0;
    gBC.gridwidth = 3;
    gBC.gridx = 0;
    gBC.gridy = 1;
    pane.add(jbnButton, gBC);

}

private static void createAndShowGUI() {

    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("GridBagLayout Source Demo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Set up the content pane.
    addComponentsToPane(frame.getContentPane());

    frame.pack();
    frame.setVisible(true);
}

public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
 }
}

这段代码可以完成工作:

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

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;

public class MyProblem {

    private JFrame frame = new JFrame();

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

    public MyProblem() {
        frame.setLayout(new GridBagLayout());
        GridBagConstraints gc = new GridBagConstraints();
        gc.weightx = 1;
        gc.weighty = 0;
        gc.gridx = 0;
        gc.gridy = 0;
        gc.ipadx = 0;
        gc.ipady = 0;
        gc.fill = GridBagConstraints.HORIZONTAL;
        gc.anchor = GridBagConstraints.NORTH;
        for (int i = 0; i < 3; i++) {
            JButton button = new JButton("Button " + i);
            frame.add(button, gc);
            gc.gridy++;
        }
        gc.weighty = 1;
        frame.add(Box.createGlue(), gc); //Adding a component to feel the area.
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500, 500);
        frame.setVisible(true);
    }

}