无法在 GridBagLayout 中跨越和左对齐文本字段
Can't span and left align text field in GridBagLayout
我有这个代码:
import javax.swing.*;
import java.awt.*;
public class Test extends JFrame {
public Test() {
setLocationByPlatform(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setLocationByPlatform(true);
GridBagConstraints constraints = new GridBagConstraints();
constraints.anchor = GridBagConstraints.LINE_START;
constraints.insets = new Insets(5, 5, 0, 5);
JRadioButton button1 = new JRadioButton("Aaaaaaaaa");
JRadioButton button2 = new JRadioButton("Bbbb");
JRadioButton button3 = new JRadioButton("cccccC");
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
panel.add(new JLabel("Test"), createConstraints(0, 0));
panel.add(button1, createConstraints(0, 1));
panel.add(button2, createConstraints(1, 1));
panel.add(button3, createConstraints(2, 1));
JTextField text = new JTextField();
GridBagConstraints c = createConstraints(0, 2);
c.gridwidth = 3;
panel.add(text, c);
add(panel, BorderLayout.PAGE_START);
setSize(350, 360);
setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
Test frame = new Test();
frame.setVisible(true);
});
}
private GridBagConstraints createConstraints(int x, int y) {
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.LINE_START;
c.insets = new Insets(5, 5, 0, 5);
c.gridx = x;
c.gridy = y;
return c;
}
}
它创建:
但我需要文本字段始终跨越 2 列并且所有内容都左对齐:
我该怎么做?
c.gridwidth = 3;
需要:
c.gridwidth = 2; // unless it should fill all THREE columns!
c.fill = GridBagConstraints.HORIZONTAL;
结果:
编辑
顺便说一句。 'Spanning two cells' 似乎是一种完全随意的调整文本字段大小的方法。最好在构造时指定列数(大致翻译为字符数),将其放在自己的一行中(跨越 3 个单元格宽度),并且 not 指定它填充该行。
我有这个代码:
import javax.swing.*;
import java.awt.*;
public class Test extends JFrame {
public Test() {
setLocationByPlatform(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setLocationByPlatform(true);
GridBagConstraints constraints = new GridBagConstraints();
constraints.anchor = GridBagConstraints.LINE_START;
constraints.insets = new Insets(5, 5, 0, 5);
JRadioButton button1 = new JRadioButton("Aaaaaaaaa");
JRadioButton button2 = new JRadioButton("Bbbb");
JRadioButton button3 = new JRadioButton("cccccC");
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
panel.add(new JLabel("Test"), createConstraints(0, 0));
panel.add(button1, createConstraints(0, 1));
panel.add(button2, createConstraints(1, 1));
panel.add(button3, createConstraints(2, 1));
JTextField text = new JTextField();
GridBagConstraints c = createConstraints(0, 2);
c.gridwidth = 3;
panel.add(text, c);
add(panel, BorderLayout.PAGE_START);
setSize(350, 360);
setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
Test frame = new Test();
frame.setVisible(true);
});
}
private GridBagConstraints createConstraints(int x, int y) {
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.LINE_START;
c.insets = new Insets(5, 5, 0, 5);
c.gridx = x;
c.gridy = y;
return c;
}
}
它创建:
但我需要文本字段始终跨越 2 列并且所有内容都左对齐:
我该怎么做?
c.gridwidth = 3;
需要:
c.gridwidth = 2; // unless it should fill all THREE columns!
c.fill = GridBagConstraints.HORIZONTAL;
结果:
编辑
顺便说一句。 'Spanning two cells' 似乎是一种完全随意的调整文本字段大小的方法。最好在构造时指定列数(大致翻译为字符数),将其放在自己的一行中(跨越 3 个单元格宽度),并且 not 指定它填充该行。