如何使JTextField更小,而不是填满整个空间?

时间:2015-02-22 23:03:01

标签: java swing

这里我对输入区域有一个简单的buildPanel方法。 我尝试使用GridLayout(7,1),但前4 JTextField似乎填满了整个空间,看起来非常难看。

有没有办法将它们改回正常大小?

private void buildInputPenal() 
{
    dateField = new JTextField(10); //dateField should be changed to something else later
    fNameField = new JTextField(15);
    lNameField = new JTextField(15);
    pledgeField = new JTextField(10);

    charityRB1 = new JRadioButton(Charity[0]);
    charityRB2 = new JRadioButton(Charity[1]);
    charityRB3 = new JRadioButton(Charity[2]);

    radioButtonGroup = new ButtonGroup();
    radioButtonGroup.add(charityRB1);
    radioButtonGroup.add(charityRB2);
    radioButtonGroup.add(charityRB3);

    charityRB1.addActionListener(new RadioButtonListener());
    charityRB2.addActionListener(new RadioButtonListener());
    charityRB3.addActionListener(new RadioButtonListener());

    inputPanel = new JPanel(new GridLayout(7,1));

    inputPanel.add(dateField);
    inputPanel.add(fNameField);
    inputPanel.add(lNameField);
    inputPanel.add(pledgeField);
    inputPanel.add(charityRB1);
    inputPanel.add(charityRB2);
    inputPanel.add(charityRB3);
}

1 个答案:

答案 0 :(得分:1)

不要使用GridLayoutGridLayout为每个组件创建一组偶数单元格(即它们获得相同的空间量)。

相反,请考虑使用GridBagLayout代替

有关详细信息,请参阅How to Use GridLayoutHow to Use GridBagLayout