【发布时间】:2019-10-20 16:50:18
【问题描述】:
我需要创建一个程序来将所有单词存储在一个数组列表中。然后检查文本字段中的用户输入,看看它是否以数字和标点符号以外的任何内容开头。否则将需要显示错误并防止将字符串添加到arraylist并显示适当的错误。
继承代码的 ActionEvent 侦听器继续检查。我不确定如何让它工作。
@Override
public void actionPerformed(ActionEvent e) {
for(int i = 0; i < 1; i++) {
String str = tf.getText(); // MUST BE STORED ON AN ARRAY LIST
ta.append(str + "\n"); // Append the text on new line each
if(str.startsWith(String.valueOf(nums))) { // Check input for a number at the start
error.setText("Error: Word starts a number. Please try again!");
error.setForeground(Color.RED);
ta.append("");
} else if (str.startsWith(String.valueOf(punct))) { // Check if input contains a punctuation at the start
error.setText("Error: Word starts with an illegal character. Please try again!");
error.setForeground(Color.RED);
ta.append("");
}
}
}
【问题讨论】:
标签: java arraylist actionlistener actionevent