无法在textArea中显示String数组项

时间:2014-12-13 22:28:39

标签: java arrays string jtextarea

我似乎无法弄清楚为什么方法display()不会在stringList中显示数组值。我已将数组设为全局但不会改变结果。

public void actionPerformed(ActionEvent event){
        display();
    }

private void display(){
    String[] stringList = {"a", "b", "c", "d", "e"};
    ArrayText.setText("");
    for (int n = 0; n < 10; n++){
        ArrayText.append(stringList[n] + "\n");
    }
}   

2 个答案:

答案 0 :(得分:0)

stringList的大小为5.您正在调用stringList [n],其中n大于4,这是非法的。

改为使用:

for (int n = 0; n < stringList.length; n++){

答案 1 :(得分:0)

你必须在ArrayText上做repaint()

private void display(){
    String[] stringList = {"a", "b", "c", "d", "e"};
    ArrayText.setText("");
    for (int n = 0; n < stringList.lenght; n++){
        ArrayText.append(stringList[n] + "\n");
    }
    ArrayText.repaint();
}   

另外,请记住有关stringList.lenght

的Paco建议