使 GridBagLayout 左对齐并正确调整 JTextBox 的大小

Getting GridBagLayout Left Justified and JTextBox sized properly

我承认 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。

那么,获得理想结果的秘诀是什么?使用此布局时要牢记任何好的提示?

哦,可运行代码

您问题的基本答案是利用 weightx 属性。基本上,您想将所有剩余的水平 space 分配给组合框和文本字段

我也会摆脱 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);

    }
}

作为一个小注意事项,您还应该使用“线”锚而不是罗盘锚。这使得采用 UI 到 RTL 语言变得更容易(我知道,你这样做的可能性有多大)

在这种情况下,只需将 GridBagConstraints.WEST 更改为 GridBagConstraints.LINE_START

即可