[英]How can I resize a Swing text field?
我寫了下面的代碼:
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);
}
}
我希望該文本字段以我最喜歡的大小顯示,但它會填滿整個屏幕。
我用了
textField[i].setAlignmentX(2f);
textField[i].setAlignmentY(2f);
和setcolum()
,但是它沒有調整文本字段的大小。 如何減小文本字段的大小?
我從
setMaximumSize(new Dimension(80, 70));
setPreferredSize(new Dimension(50, 50));
他們調整了文本框的大小,但更改了它的位置,但我不想更改其
location。除了我更改文本字段位置以外,還有什么方法嗎?
我從setlocation使用,但是沒有用!
我認為您想為文本字段設置特定的大小。 為此:首先使用Information類的對象將布局設置為null。
information.setLayout(null);
並使用setBounds方法設置JTextField的邊界:
textField.setBounds(0,0,100,100);
我相信您想使用setBounds
,它將寬度和高度作為參數。
setAlignment
方法更改組件在其容器中的對齊方式,而setAlignment
更改組件的實際大小。 我認為您正在尋找JTextField
的setColumns()
方法 。 總是還有繼承的setSize()
方法 。
嘗試使用setPreferredSize() (與setMinimum / MaximumSize()一起使用),往往會被包括Layout在內的許多LayoutManager使用,以適當調整容器組件的大小。
另外,作為一個旁注,看起來您正在for循環的每個迭代中重新創建JTextAreas數組,因此僅存在textField [19]。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.