ArrayList <String> cdcollection = new ArrayList();

private void initButtonActionPerformed(java.awt.event.ActionEvent evt) {
    Collections.addAll(cdcollection, "small", "mayre", "brown", "evner", "rain" );
    initButton.setEnabled(false);

}

private void displayButtonActionPerformed(java.awt.event.ActionEvent evt) {

          for (int i = 0; i < cdcollection.size(); i++)  {
          mainTextArea.setText(cdcollection.get(i));
    }
}

private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {
    cdcollection.add(cdtitleInput.getText());
}

private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) {
    cdcollection.remove(cdcollection.size()-1);
}

当我运行此命令并单击显示按钮时,仅显示最后一个CD标题(雨)...
如何使所有五个CD标题都显示在一行上?

最佳答案

更改displayButtonActionPerformed方法,如下所示。

private void displayButtonActionPerformed(java.awt.event.ActionEvent evt) {
    String str = "";
    for (int i = 0; i < cdcollection.size(); i++)  {
          str = str + cdcollection.get(i);
    }
    mainTextArea.setText(str);
}

07-27 21:06