我承认GridBagLayout对我来说可能是个脑筋。
用不同的设置进行测试,除了我想要的结果外,还会给出不同的结果。
下面的代码示例使用自己的GridBagConstraints实例来帮助确保每个实例都有自己的特定设置。
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class GridLayout2ColumnTest
{
public static void main(String[] args) {
JFrame aWindow = new JFrame();
aWindow.setBounds(200, 200, 400, 400);
aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = aWindow.getContentPane();
content.add(new GridBagLayoutPanel2());
aWindow.setVisible(true);
}
}
class GridBagLayoutPanel2 extends JPanel {
public GridBagLayoutPanel2() {
GridBagLayout gridbag = new GridBagLayout();
setLayout(gridbag);
GridBagConstraints gbc1 = new GridBagConstraints();
gbc1.gridx = 0;
gbc1.gridy = 0;
gbc1.anchor = GridBagConstraints.WEST;
gbc1.fill = GridBagConstraints.BOTH;
gbc1.insets = new Insets(5, 5, 5, 5);
JLabel jLabel = new JLabel("Lable 1");
gridbag.setConstraints(jLabel, gbc1);
add(jLabel);
GridBagConstraints gbc2 = new GridBagConstraints();
gbc2.gridx = 1;
gbc2.gridy = 0;
gbc2.anchor = GridBagConstraints.WEST;
gbc2.fill = GridBagConstraints.BOTH;
gbc2.insets = new Insets(5, 5, 5, 5);
JComboBox jcb = new JComboBox();
jcb.addItem("This is a long entry for Row 1");
gridbag.setConstraints(jcb, gbc2);
add(jcb);
GridBagConstraints gbc3 = new GridBagConstraints();
gbc3.gridx = 0;
gbc3.gridy = 1;
gbc3.anchor = GridBagConstraints.WEST;
gbc3.fill = GridBagConstraints.BOTH;
gbc3.insets = new Insets(5, 5, 5, 5);
JLabel jLabel2 = new JLabel("Lable 2");
gridbag.setConstraints(jLabel2, gbc3);
add(jLabel2);
GridBagConstraints gbc4 = new GridBagConstraints();
gbc4.gridx = 1;
gbc4.gridy = 1;
gbc4.anchor = GridBagConstraints.WEST;
gbc4.fill = GridBagConstraints.BOTH;
gbc4.insets = new Insets(5, 5, 5, 5);
JTextField jtf = new JTextField(5);
gridbag.setConstraints(jtf, gbc4);
add(jtf);
}
}
上面的代码生成以下内容。
我想要的是以下结果。
我确实读过不应该使用setPreferredSize和setMinimumSize。
那么,在使用此布局时,获得所需结果的秘诀是什么以及任何需要记住的好提示呢?
发布于 2022-01-24 23:16:02
哦,可运行的代码
您问题的基本答案是使用weightx
属性。基本上,您希望将所有剩余的水平空间分配给组合框和文本字段。
我还会去掉fill
属性,因为它不会生成您想要的结果。
class GridBagLayoutPanel2 extends JPanel {
public GridBagLayoutPanel2() {
GridBagLayout gridbag = new GridBagLayout();
setLayout(gridbag);
GridBagConstraints gbc1 = new GridBagConstraints();
gbc1.gridx = 0;
gbc1.gridy = 0;
gbc1.anchor = GridBagConstraints.WEST;
//gbc1.fill = GridBagConstraints.BOTH;
gbc1.insets = new Insets(5, 5, 5, 5);
JLabel jLabel = new JLabel("Lable 1");
gridbag.setConstraints(jLabel, gbc1);
add(jLabel);
GridBagConstraints gbc2 = new GridBagConstraints();
gbc2.gridx = 1;
gbc2.gridy = 0;
gbc2.anchor = GridBagConstraints.WEST;
//gbc2.fill = GridBagConstraints.BOTH;
gbc2.insets = new Insets(5, 5, 5, 5);
gbc2.weightx = 1;
JComboBox jcb = new JComboBox();
jcb.addItem("This is a long entry for Row 1");
gridbag.setConstraints(jcb, gbc2);
add(jcb);
GridBagConstraints gbc3 = new GridBagConstraints();
gbc3.gridx = 0;
gbc3.gridy = 1;
gbc3.anchor = GridBagConstraints.WEST;
//gbc3.fill = GridBagConstraints.BOTH;
gbc3.insets = new Insets(5, 5, 5, 5);
JLabel jLabel2 = new JLabel("Lable 2");
gridbag.setConstraints(jLabel2, gbc3);
add(jLabel2);
GridBagConstraints gbc4 = new GridBagConstraints();
gbc4.gridx = 1;
gbc4.gridy = 1;
gbc4.anchor = GridBagConstraints.WEST;
//gbc4.fill = GridBagConstraints.BOTH;
gbc4.insets = new Insets(5, 5, 5, 5);
gbc4.weightx = 1;
JTextField jtf = new JTextField(5);
gridbag.setConstraints(jtf, gbc4);
add(jtf);
}
}
作为一个次要的注意事项,你也应该使用“线”锚而不是指南针。这使得RTL语言更容易采用UI (我知道,您这么做的可能性有多大)
在本例中,它就像将GridBagConstraints.WEST
更改为GridBagConstraints.LINE_START
一样简单
https://stackoverflow.com/questions/70841582
复制相似问题