Java 添加JComboBox和JButton时GridBagLayout出现问题

Java 添加JComboBox和JButton时GridBagLayout出现问题,java,swing,layout-manager,gridbaglayout,insets,Java,Swing,Layout Manager,Gridbaglayout,Insets,在输出中,我不需要按钮4(b4)和按钮5(b5)之间的间隙,但我无法做到这一点,我也尝试了插入,但我的尝试是徒劳的。你能帮我解决一些建议或我犯过的错误吗 我注意到,当我同时添加JComboBox和JButton时,就会出现这个问题 甚至我也无法通过setBounds()或Inset类设置插入或边界 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class FontWindow { Fon

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

我注意到,当我同时添加
JComboBox
JButton
时,就会出现这个问题

甚至我也无法通过
setBounds()
Inset
类设置插入或边界

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
s,您可以使用更简单的布局管理器获得更好的控制
例如,用
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);
JPanel p=newjpanel();
p、 setLayout(新的GridBagLayout());
GridBagConstraints gbc=新的GridBagConstraints();
JPanel-topRowPane=新的JPanel()//默认情况下使用FlowLayout
gbc.gridx=0;
gbc.gridy=0;
p、 添加(顶行窗格,gbc);
字符串clist1[]={“Courier New”、“Times New Roman”、“Calibri”};
字符串clist2[]={“8”、“9”、“10”、“11”、“12”、“14”、“16”、“18”、“20”、“22”、“24”、“26”、“28”、“36”、“72”};
JComboBox c1=新的JComboBox(clist1);
JComboBox c2=新的JComboBox(clist2);
顶行窗格。添加(c1);
顶行窗格。添加(c2);
JButton b1=新JButton(“b1”);
JButton b2=新JButton(“b2”);
JButton b3=新JButton(“b3”);
顶行窗格。添加(b1);
topRowPane.add(b2);
topRowPane.add(b3);
JPanel bottomRowPane=新的JPanel();
gbc.anchor=GridBagConstraints.LINE\u START;
gbc.gridx=0;
gbc.gridy=1;
p、 添加(底部行窗格,gbc);
JButton b4=新JButton(“b4”);
JButton b5=新JButton(“b5”);
JButton b6=新JButton(“b6”);
底部行窗格。添加(b4);
底部行窗格。添加(b5);
底部行窗格。添加(b6);
1)提供最小尺寸的ASCII艺术或GUI预期布局的简单绘图,如果可以调整大小,则提供更宽和更高的宽度和高度,以显示应如何使用额外的空间。2)
f.setSize(600400);f、 加(p)应改为
f.add(p);f、 pack();/*将GUI大小设置为显示组件所需的最小值*/