【问题标题】:Java - Can't get getText in jTextArea to work properlyJava - 无法让 jTextArea 中的 getText 正常工作
【发布时间】: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("") &amp;&amp; !jTextArea1.equals(null) 永远为真(因为 JTextArea != String 和 jTextArea1 != null )

标签: java swing arraylist jtextarea


【解决方案1】:

读取api doc of getText(int, int):第二个参数不是偏移量。这是一个长度。

附注 1:将所有文本作为单个字符串并在换行符上拆分,并将每个字符串解析为整数可能会更容易。

旁注 2:测试if (jTextArea1.equals("")) 无法成功。 JTextArea 实例永远不会等于 String 实例。

【讨论】:

  • 使用了 Scanner 类及其方法。感谢您的提示 - 事实证明它更容易。
【解决方案2】:

我没有检查完整的程序,但这是错误的:

if(!jTextArea1.equals("") && !jTextArea1.equals(null)){

您忘记添加getText() 的电话了吗?该行将始终被评估为true,因为JTextArea 的实例对象永远不会等于""null。后者意味着jTextArea1 对象本身为空。当您调用 equals 方法时,这会给您一个 NPE。

【讨论】:

  • 不敢相信我没有发现这个大声笑...谢谢大家的投入.. 深夜编码.. 编辑:记住调用 getText( ) n = Integer.parseInt(jTextArea1.getText(jTextArea1.getLineStartOffset(i),jTextArea1.getLineEndOffset(i))); 有什么问题吗?
  • 是的,有问题。 getText 的 sscond 参数不正确。请参阅我自己的答案。
  • @JB Nizet 我仍然不明白为什么它不应该工作。我可以看到第二个参数是一个长度,但这本质上不是 getLineEndOffset() 得到的吗?我对此感到很困惑。有什么建议吗?
  • 行尾偏移量是相对于文本区域开头的偏移量,而不是相对于行首的偏移量。因此,如果您的 lin 从偏移量 30 开始并且长度为 5 个字符,则 getLineEndOffset 将返回 35。因此您将传递 35 而不是 5 作为长度。您为什么不自己尝试一下和/或使用调试器看看会发生什么?
【解决方案3】:

在检查条件之前是否重置标志?考虑以下情况:

   //suppose errorsHappened is true here 
   if(jCheckBox5.isSelected()){ //we get true here
        checkWorld = true;
        if(!worldsChosen.isEmpty()){ //not empty, so this branch is taken
            changeWorlds = true;
        }else{ //worldsChosen is not empty, so this would not be logged
            log("You've selected the option for automatic world switching,");
            log("but all of your inputs weren't formatted correctly.");
            errorsHappened = true;
        }
    }else{ //checkbox is selected, so no reset to false here
        errorsHappened = false;
    }
   //due to the checkbox being selected and worldsChosen not being empty, 
   //errorsHappend is still true (which is wrong)

【讨论】:

    猜你喜欢
    • 2021-05-04
    • 2012-09-05
    • 2017-11-06
    • 2011-09-06
    • 1970-01-01
    • 2023-03-10
    • 2020-10-16
    • 2014-01-15
    • 2019-07-29
    相关资源
    最近更新 更多