首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使GridBagLayout左对齐并适当调整JTextBox大小

使GridBagLayout左对齐并适当调整JTextBox大小
EN

Stack Overflow用户
提问于 2022-01-24 23:09:08
回答 1查看 32关注 0票数 0

我承认GridBagLayout对我来说可能是个脑筋。

用不同的设置进行测试,除了我想要的结果外,还会给出不同的结果。

下面的代码示例使用自己的GridBagConstraints实例来帮助确保每个实例都有自己的特定设置。

代码语言:javascript
运行
复制
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。

那么,在使用此布局时,获得所需结果的秘诀是什么以及任何需要记住的好提示呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-24 23:16:02

哦,可运行的代码

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

我还会去掉fill属性,因为它不会生成您想要的结果。

代码语言:javascript
运行
复制
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一样简单

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70841582

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档