JTextField大小随JLabel文本长度而变化

时间:2014-11-11 13:22:31

标签: java swing gridbaglayout

我想连续创建3个JTextFields,其大小相同。但是我的第一个Text字段比其他两个字段宽。
在一次添加每个组件并查看导致问题的原因之后,我意识到如果我在发生此问题时为JLabel键入一个长文本。如果我减少JLabel中文本的长度,则JTextFields进入我想要的尺寸。这是我的代码

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.Math;
public class Calculator1 extends JFrame {

JTextField value1=new JTextField("0");
JTextField value2=new JTextField("0");
JTextField value3=new JTextField("0");
JLabel label1=new JLabel("Label1 size of JTextfields chnge with Label length");
JTextField value4=new JTextField("0");
JTextField value5=new JTextField("0");
JTextField value6=new JTextField("0");
JLabel label2=new JLabel("Label2");
JLabel label3=new JLabel("Label3");
JTextField value7=new JTextField("0");
public Calculator1(){

    super("Calculator");

    setSize(500,200);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel=new JPanel();
    GridBagLayout bag=new GridBagLayout();
    getContentPane().add(panel);
    panel.setLayout(bag);
    GridBagConstraints c=new GridBagConstraints();

    c.insets=new Insets(5,5,5,5);
    c.fill=GridBagConstraints.HORIZONTAL;


    c.gridx=0;
    c.gridy=0;
    c.weightx=0.0;
    c.weighty=0.5;
    c.gridwidth=1;
    panel.add(label1,c);


    c.gridx=0;
    c.gridy=1;
    c.gridwidth=5;
    c.weightx=0.5;
    c.weighty=0.5;
    panel.add(value1,c);


    c.gridx=5;
    c.gridy=1;
    panel.add(value2,c);


    c.gridx=10;
    c.gridy=1;
    panel.add(value3,c);

    c.gridwidth=1;
    c.weightx=0.0;
    c.weighty=0.5;
    c.gridx=0;
    c.gridy=3;
    panel.add(label2,c);

    c.gridwidth=5;
    c.weightx=0.5;
    c.weighty=0.5;
    c.gridx=0;
    c.gridy=4;
    c.insets=new Insets(5,5,5,5);
    panel.add(value4,c);


    c.gridx=5;
    c.gridy=4;
    panel.add(value5,c);


    c.gridx=10;
    c.gridy=4;
    panel.add(value6,c);

    c.gridwidth=1;
    c.weightx=0.0;
    c.weighty=0.5;
    c.gridx=0;
    c.gridy=5;
    panel.add(label3,c);

    c.gridwidth=5;
    c.weightx=0.5;
    c.weighty=0.5;
    c.gridx=0;
    c.gridy=6;
    panel.add(value7,c);


    setVisible(true);
}
public static void main (String args[]){
    Calculator1 cal=new Calculator1();
}

}

当我更改JLabel label1的文本长度时,如何停止更改JTextFields的大小

2 个答案:

答案 0 :(得分:2)

gridwidth = 3设置JLabel,为JTextField设置1,如下:

public Calculator() {

    super("Calculator");

    setSize(500, 200);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel();
    GridBagLayout bag = new GridBagLayout();
    getContentPane().add(panel);
    panel.setLayout(bag);
    GridBagConstraints c = new GridBagConstraints();

    c.insets = new Insets(5, 5, 5, 5);
    c.fill = GridBagConstraints.HORIZONTAL;

    c.gridx = 0;
    c.gridy = 0;
    c.weightx = 0.0;
    c.weighty = 0.5;
    c.gridwidth = 3;
    panel.add(label1, c);

    c.gridx = 0;
    c.gridy = 1;
    c.gridwidth = 1;
    c.weightx = 0.5;
    c.weighty = 0.5;
    panel.add(value1, c);

    c.gridx = 1;
    c.gridy = 1;
    panel.add(value2, c);

    c.gridx = 2;
    c.gridy = 1;
    panel.add(value3, c);

    c.gridwidth = 3;
    c.weightx = 0.0;
    c.weighty = 0.5;
    c.gridx = 0;
    c.gridy = 3;
    panel.add(label2, c);

    c.gridwidth = 1;
    c.weightx = 0.5;
    c.weighty = 0.5;
    c.gridx = 0;
    c.gridy = 4;
    c.insets = new Insets(5, 5, 5, 5);
    panel.add(value4, c);

    c.gridx = 1;
    c.gridy = 4;
    panel.add(value5, c);

    c.gridx = 2;
    c.gridy = 4;
    panel.add(value6, c);

    c.gridwidth = 3;
    c.weightx = 0.0;
    c.weighty = 0.5;
    c.gridx = 0;
    c.gridy = 5;
    panel.add(label3, c);

    c.gridwidth = 1;
    c.weightx = 0.5;
    c.weighty = 0.5;
    c.gridx = 0;
    c.gridy = 6;
    panel.add(value7, c);

    setVisible(true);
}

enter image description here

另请阅读GridBagLayout

的教程

答案 1 :(得分:0)

使用网格布局。如果你不提供尺寸,它会自动使所有人都平等。 例如:

setLayout(new GridLayout(rows,cols,vgap, hagap))