添加 JComboBox 和 JButton 时 GridBagLayout 出现问题

Problem with GridBagLayout while adding JComboBox and JButton

在输出中,我不需要按钮 4 (b4) 和按钮 5 (b5) 之间的间隙,但我做不到,我也尝试了插入,但是我的尝试是徒劳的。你能帮我解决任何建议或我犯的任何错误吗?

我观察到当我同时添加 JComboBoxJButton 时会出现此问题。

即使我无法通过 setBounds()Inset class 设置插图或边界。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class FontWindow {
    FontWindow() {
        JFrame f = new JFrame();
        JPanel p = new JPanel();
        p.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        JComboBox c1,c2;
        String clist1[] = {"Courier New","Times New Roman","Calibri"};
        String clist2[] = {"8","9","10","11","12","14","16","18","20","22","24","26","28","36","72"};
        c1 = new JComboBox(clist1);
        c2 = new JComboBox(clist2);

        gbc.gridx = 0;
            gbc.gridy = 0;
            p.add(c1,gbc);

        gbc.gridx = 1;
            gbc.gridy = 0;
            p.add(c2,gbc);

        JButton b1,b2,b3;
        b1 = new JButton("B1");
        b2 = new JButton("B2");
        b3 = new JButton("B3");

        //gbc.insets = new Insets(0,5,0,0);
        gbc.gridx = 2;
            gbc.gridy = 0;
            p.add(b1,gbc);

        gbc.gridx = 3;
            gbc.gridy = 0;
            p.add(b2,gbc);

        gbc.gridx = 4;
            gbc.gridy = 0;
            p.add(b3,gbc);

        JButton b4,b5,b6;

        b4 = new JButton("B4");
        b5 = new JButton("B5");
        b6 = new JButton("B6");

        //gbc.insets = new Insets(0,10,0,5);
        gbc.anchor = GridBagConstraints.BELOW_BASELINE_LEADING;
        gbc.gridx = 0;
            gbc.gridy = 1;
            p.add(b4,gbc);
        gbc.gridx = 1;
            gbc.gridy = 1;
            p.add(b5,gbc);
        gbc.gridx = 2;
            gbc.gridy = 1;
            p.add(b6,gbc);

        f.setSize(600,400);
        f.add(p);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }
    public static void main(String args[]) {
        new FontWindow();
    }
}

这是我得到的输出:

欢迎来到 SO。查看评论以了解约束的变化:

    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.gridwidth = 3; //c1 is wide so let it span over 3 columns
    p.add(c1,gbc);

    gbc.gridwidth = 1;//reset span to 1
    gbc.gridx = 3;    //amend column numbers of row 0
    gbc.gridy = 0;
    p.add(c2,gbc);

    JButton b1,b2,b3;
    b1 = new JButton("B1");
    b2 = new JButton("B2");
    b3 = new JButton("B3");

    gbc.gridx = 4;
    gbc.gridy = 0;
    p.add(b1,gbc);

    gbc.gridx = 5;
    gbc.gridy = 0;
    p.add(b2,gbc);

    gbc.gridx = 6;
    gbc.gridy = 0;
    p.add(b3,gbc);

    //no change at the rest of the code 

通过将布局划分为更小、更易于管理的 JPanel,您可以使用更简单的布局管理器来更好地控制。
例如,用 JPanel 包装每个组件行:

    JPanel p = new JPanel();
    p.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();

    JPanel topRowPane = new JPanel();//uses FlowLayout by default
    gbc.gridx = 0;
    gbc.gridy = 0;
    p.add(topRowPane,gbc);

    String clist1[] = {"Courier New","Times New Roman","Calibri"};
    String clist2[] = {"8","9","10","11","12","14","16","18","20","22","24","26","28","36","72"};
    JComboBox<String> c1 = new JComboBox<>(clist1);
    JComboBox<String> c2 = new JComboBox<>(clist2);

    topRowPane.add(c1);
    topRowPane.add(c2);

    JButton b1 = new JButton("B1");
    JButton b2 = new JButton("B2");
    JButton b3 = new JButton("B3");
    topRowPane.add(b1);
    topRowPane.add(b2);
    topRowPane.add(b3);

    JPanel bottomRowPane = new JPanel();
    gbc.anchor = GridBagConstraints.LINE_START;
    gbc.gridx = 0;
    gbc.gridy = 1;
    p.add(bottomRowPane,gbc);

    JButton b4 = new JButton("B4");
    JButton b5 = new JButton("B5");
    JButton b6 = new JButton("B6");
    bottomRowPane.add(b4);
    bottomRowPane.add(b5);
    bottomRowPane.add(b6);