简体   繁体   English

如何调整Swing文本字段的大小?

[英]How can I resize a Swing text field?

I wrote the code below: 我写了下面的代码:

public class Information extends JFrame{
private JComboBox comboBox1;
private JComboBox comboBox2;
private JTextField textField[];
private JTextField jtextField;
private JLabel label[];

public Information()
{
    setLayout(new flowlayout());
    Box box = Box.createVerticalBox();
    for(int i = 0; i < 20; i++)//textfield
    {
        textField = new JTextField[20];
        box.add(Box.createRigidArea(new Dimension(5,5)));
        textField[i] = new JTextField(2);

        textField[i].setAlignmentX(2f);
        textField[i].setAlignmentY(2f);

        box.add(textField[i]);
    }
    for(int i = 0; i < 22; i++ )//lable
    {

    }
    add(box);
}
}

I want that text field showed in my favorite size, but it fills the whole screen. 我希望该文本字段以我最喜欢的大小显示,但它会填满整个屏幕。

I used 我用了

 textField[i].setAlignmentX(2f);
 textField[i].setAlignmentY(2f);

and setcolum() , but it didn't resize the text field. setcolum() ,但是它没有调整文本字段的大小。 How can I decrease my text fields' sizes? 如何减小文本字段的大小?

I use from 我从

setMaximumSize(new Dimension(80, 70));
setPreferredSize(new Dimension(50, 50));

their resize the textfield but change it's location but i don't want to change their 他们调整了文本框的大小,但更改了它的位置,但我不想更改其

location.is there any manner than i change textfield position? location。除了我更改文本字段位置以外,还有什么方法吗?

i use from setlocation but it didn't work!!. 我从setlocation使用,但是没有用!

I think you want to Set a specific size for your textfield. 我认为您想为文本字段设置特定的大小。 To do this : Firstly set the layout as null using the object of Information class 为此:首先使用Information类的对象将布局设置为null。

information.setLayout(null);

And set the bounds of the JTextField using setBounds method : 并使用setBounds方法设置JTextField的边界:

textField.setBounds(0,0,100,100);

我相信您想使用setBounds ,它将宽度和高度作为参数。

The setAlignment methods change the alignment of a component in its container, not the component's actual size. setAlignment方法更改组件在其容器中的对齐方式,而setAlignment更改组件的实际大小。 I think you're looking for JTextField 's setColumns() method . 我认为您正在寻找JTextFieldsetColumns()方法 There's always the inherited setSize() method , too. 总是还有继承的setSize()方法

Try setPreferredSize() , this (along with setMinimum/MaximumSize()) tend to be used by a lot of LayoutManagers including FlowLayout to appropriately size a container's components. 尝试使用setPreferredSize() (与setMinimum / MaximumSize()一起使用),往往会被包括Layout在内的许多LayoutManager使用,以适当调整容器组件的大小。

Also, as a side note, looks like you're re-creating your array of JTextAreas each iteration in that for loop, so only textField[19] would exist... 另外,作为一个旁注,看起来您正在for循环的每个迭代中重新创建JTextAreas数组,因此仅存在textField [19]。

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

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