【问题标题】:How to convert from String[] to JTextField[]如何从 String[] 转换为 JTextField[]
【发布时间】:2015-06-04 16:38:29
【问题描述】:

我正在尝试根据从文件输入的字长创建一个 JTextField 数组。我似乎找不到从 String[] 转换为 JTextField[] 的方法。

//Splits the word chosen from word list into an array
JTextField[] wordAmount = new JTextField[word.length()];//Creates a JtextField for each letter
String[] items = word.split("");

String temp;
for (int j = 0; j < items.length; j ++) {
    temp = items[j];
    wordAmount[j].setText(temp);
}

【问题讨论】:

    标签: java string jtextfield


    【解决方案1】:

    您需要在调用 JTextField 数组中的每个 JTextField 方法之前对其进行初始化,因此在调用 setText() 方法之前添加此语句

    wordAmount[j] = new JTextField();
    

    所以把你的 for 循环改成这个

    for (int j = 1; j < items.length; j++) {
                temp = items[j];
                wordAmount[j] = new JTextField();
                wordAmount[j].setText(temp);
    
    }
    

    【讨论】:

    • 谢谢你,你是最棒的
    猜你喜欢
    • 1970-01-01
    • 2015-04-02
    • 2011-05-05
    • 1970-01-01
    • 2011-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多