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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5487270/
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-30 11:20:47  来源:igfitidea点击:

How can I resize a Swing text field?

javatextfieldjtextfield

提问by Mahdi_Nine

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. How can I decrease my text fields' sizes?

setcolum(),但它没有调整文本字段的大小。如何减小文本字段的大小?

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 使用,但没有用!!

回答by Herrad

Try setPreferredSize(), this (along with setMinimum/MaximumSize()) tend to be used by a lot of LayoutManagersincluding FlowLayout to appropriately size a container's components.

尝试setPreferredSize(),它(连同 setMinimum/MaximumSize())往往被许多LayoutManager使用,包括 FlowLayout 来适当地调整容器组件的大小。

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] 将存在...

回答by Mayank Jain

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.setLayout(null);

And set the bounds of the JTextField using setBounds method :

并使用 setBounds 方法设置 JTextField 的边界:

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

回答by Riggy

I believe you want to use setBoundswhich will take a width and height as parameters.

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

回答by Pops

The setAlignmentmethods change the alignment of a component in its container, not the component's actual size. I think you're looking for JTextField's setColumns()method. There's always the inherited setSize()method, too.

这些setAlignment方法会更改组件在其容器中的对齐方式,而不是组件的实际大小。我想你正在寻找JTextFieldsetColumns()方法。也总是有继承的setSize()方法