java Swing GridBagLayout - 自动调整字段大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14819293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:29:12  来源:igfitidea点击:

Swing GridBagLayout - auto-resize fields

javaswing

提问by tomas

This is a piece of code generated from netbeans Swing gui designer. I'm trying to do the following: jLabel1and jLabel2will contain only image icon with dimension 52x46 px without text, they should be fixed in the left and right side of the line, jTextField2is expected to fill the gap between jlabels and autoresize to full screen/view width.

这是一段由netbeans Swing gui 设计器生成的代码。我正在尝试执行以下操作:jLabel1并且jLabel2将仅包含尺寸为 52x46 px 的图像图标而没有文本,它们应该固定在行的左侧和右侧,jTextField2预计会填补 jlabels 之间的空白并自动调整为全屏/视图宽度。

Problem is, that jTextField2remains with same width no matter what size of window/view is... The initial width is dependent on the length of hard-coded text inside the field...

问题是,jTextField2无论窗口/视图的大小如何,它都保持相同的宽度......初始宽度取决于字段内硬编码文本的长度......

Do you have any idea, how to do this?

你有什么想法,如何做到这一点?

private void initComponents() {
    javax.swing.JLabel jLabel1;
    javax.swing.JLabel jLabel2;
    javax.swing.JTextField jTextField2;

    java.awt.GridBagConstraints gridBagConstraints;

    jLabel1 = new javax.swing.JLabel();
    jTextField2 = new javax.swing.JTextField();
    jLabel2 = new javax.swing.JLabel();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    getContentPane().setLayout(new java.awt.GridBagLayout());

    jLabel1.setText("ABC");
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
    getContentPane().add(jLabel1, gridBagConstraints);

    jTextField2.setText("some text field content");
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridwidth = java.awt.GridBagConstraints.RELATIVE;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    getContentPane().add(jTextField2, gridBagConstraints);

    jLabel2.setText("ABC");
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
    getContentPane().add(jLabel2, gridBagConstraints);

    pack();
}

回答by Aubin

See documentation of GridBagLayout

请参阅 GridBagLayout 的文档

GridBagConstraints.weightx, GridBagConstraints.weighty

GridBagConstraints.weightx, GridBagConstraints.weighty

Used to determine how to distribute space, which is important for specifying resizing behavior. Unless you specify a weight for at least one component in a row (weightx) and column (weighty), all the components clump together in the center of their container. This is because when the weight is zero (the default), the GridBagLayout object puts any extra space between its grid of cells and the edges of the container.

用于确定如何分配空间,这对于指定调整大小行为很重要。除非您为行 (weightx) 和列 (weighty) 中的至少一个组件指定权重,否则所有组件都会聚集在其容器的中心。这是因为当权重为零(默认值)时,GridBagLayout 对象会在其单元格网格和容器边缘之间放置任何额外的空间。

jTextField2.setText("some text field content");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridwidth = java.awt.GridBagConstraints.RELATIVE;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;

gridBagConstraints.weightx = 1.0; //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

getContentPane().add(jTextField2, gridBagConstraints);