Java Swing GridBagLayout - 添加没有空格的按钮

标签 java swing layout-manager gridbaglayout

如何消除 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);
    }

}

我的按钮是什么样子的:
enter image description here
我希望我的按钮看起来像什么: enter image description here

最佳答案

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

改变:

        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 中。边框布局的这一部分将拉伸(stretch)子组件(我们的带有 GridLayout 的面板)的宽度以填充可用空间,但它会尊重高度 中的任何内容 - 只为组件提供它需要的垂直空间。

编辑2

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

enter image description here enter image description here enter image description here

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

关于Java Swing GridBagLayout - 添加没有空格的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30642334/

相关文章:

java - 如何打开一个新的 jFrame 并拒绝访问前一个 jFrame

java - 自定义布局 Swing 应用程序

java - 背景颜色和大小相等的 jradiobuttons

java - 在填充有其他面板的卡片布局顶部显示 JPanel

Java Swing : Insert JTable column into mysql database

java - GridBagLayout 和最小宽度

java - 有条件地排除 Ivy 的某些依赖关系

java - 唯一标识一组数字,其顺序无关紧要

java - Spring WebExceptionHandler 顺序

java - Spring 安全: How to access authenticated user in asynchronous Jobs