开发者

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.c开发者_如何学运维reateRigidArea(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?

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?

i use from setlocation but it didn't work!!.


use setPreferredSize()

also setMinimumSize(), setMaximumSize()


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


The setAlignment methods 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.


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.

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...


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 :

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜