简体   繁体   English

Swing GridBagLayout - 自动调整大小字段

[英]Swing GridBagLayout - auto-resize fields

This is a piece of code generated from netbeans Swing gui designer. 这是一个由netbeans Swing gui设计师生成的代码。 I'm trying to do the following: jLabel1 and jLabel2 will contain only image icon with dimension 52x46 px without text, they should be fixed in the left and right side of the line, jTextField2 is expected to fill the gap between jlabels and autoresize to full screen/view width. 我正在尝试执行以下操作: jLabel1jLabel2将仅包含尺寸为52x46 px而没有文本的图像图标,它们应该固定在该行的左侧和右侧, jTextField2预计将填补jlabels和autoresize之间的空白全屏/视图宽度。

Problem is, that jTextField2 remains 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();
}

See documentation of GridBagLayout 请参阅GridBagLayout的文档

GridBagConstraints.weightx , GridBagConstraints.weighty GridBagConstraints.weightxGridBagConstraints.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. 除非您为一行(weightx)和列(weighty)中的至少一个组件指定权重,否则所有组件在其容器的中心聚集在一起。 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. 这是因为当权重为零(默认值)时,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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM