是否可以创建一个JTextField变量字符串

时间:2015-12-28 06:29:09

标签: java arrays swing

我通过

制作一些变量
        String[] StoreValueFromTable = new String[5];
        String[] ColumName = new String[5];
        for(int Count=0;Count<5;Count++){
            StoreValueFromTable[Count] = "QueryTechnica"+Count;
            ColumName[Count] = "QT"+Count;
            System.out.println(StoreValueFromTable[Count]+", "+ColumName[Count]);
        }

我想让JTextField[] Fiels[Count] = new JTextField();给我错误。我不确定,有没有办法让它动态化..

1 个答案:

答案 0 :(得分:3)

你可以这样做:

final int TEXTFIELDS_COUNT = 5; 
// Create an array of 5 JTextFields
JTextField[] fields = new JTextField[TEXTFIELDS_COUNT];

for(int count = 0; count< TEXTFIELDS_COUNT; count++){
    // fields[count] represents a single JTextField
    fields[count] = new JTextField();

    // Do something with fields[count], like setting its text 
    // fields[count].setText("some text");

    StoreValueFromTable[count] = "QueryTechnica"+count;
    ColumName[count] = "QT"+count;
    System.out.println(StoreValueFromTable[count]+", "+ColumName[count]);
}