在输出中,我不需要按钮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);