【发布时间】:2011-07-05 13:16:01
【问题描述】:
我有一个单独的 JFrame,其中有一个文本框 (jTextArea),它将数字作为输入,每个都用新行分隔。使用文本框关闭 JFrame 后,数据应该存储在整数的 ArrayList 中。单击主 JFrame 中的按钮时会检查 ArrayList,如果发生错误,则会记录错误。
带有 jTextArea 的 JFrame 的代码如下所示:
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
boolean success = false;
try{
selectedTime = Long.parseLong(jTextField1.getText());
if(selectedTime >= 10000){
success = true;
if(!jTextArea1.equals("") && !jTextArea1.equals(null)){
try{
for(int i = 0; i < jTextArea1.getLineCount(); i++){
n = Integer.parseInt(jTextArea1.getText(jTextArea1.getLineStartOffset(i),jTextArea1.getLineEndOffset(i)));
if(n <= 172){
worldsChosen.add(n);
}
}
}catch(Exception e){
errorsHappened = true;
}
}
}else{
javax.swing.JOptionPane.showMessageDialog(null,"The specified time was not above or equal to 10000 ms. Please try again.");
success = false;
}
}catch(Exception e){
javax.swing.JOptionPane.showMessageDialog(null,"The specified time was not set in numbers exclusively. Please try again.");
success = false;
}
if(success){
gui.hideWorlds();
}
}
注意:它还会检查文本框输入的数字是否等于或大于 10000(此方法有效)。
主JFrame的代码:
if(jCheckBox5.isSelected()){
checkWorld = true;
if(!worldsChosen.isEmpty()){
changeWorlds = true;
}else{
log("You've selected the option for automatic world switching,");
log("but all of your inputs weren't formatted correctly.");
errorsHappened = true;
}
}else{
errorsHappened = false;
}
if(errorsHappened == true){
log("One or multiple worlds weren't added due to incorrect formatting.");
log("Retry to make script automatically change worlds.");
}
每当我在选中复选框并在文本区域中正确格式化的情况下运行脚本时 (像这样):
1
2
3
4
5
等等
它输出所有的日志消息(好像复选框已被选中,但没有一个输入格式正确)。
我已尽我所能解决此问题,但我就是看不出它是如何搞砸的。
任何帮助表示赞赏:)。
迈克·海耶。
【问题讨论】:
-
"if(!jTextArea1.equals("") && !jTextArea1.equals(null))" :我想你想测试的是 jTextArea1.getText() 。顺便说一句 .equals(null) 是可憎的,替换为 ==null
-
!jTextArea1.equals("") && !jTextArea1.equals(null)永远为真(因为 JTextArea != String 和 jTextArea1 != null )
标签: java swing arraylist jtextarea