【问题标题】:javafx 8 edit text in textareajavafx 8在textarea中编辑文本
【发布时间】:2020-03-16 17:30:34
【问题描述】:

我们正在尝试更正 TextArea 中单词的拼写
我们尝试了两种方法 onRemoveTwo 失败,出现 IndexOutOfBoundsException
另一种方法 onRemove 有效,但它做了一个 replaceAll 我们在代码中使用 replace
以下是两种方法的结果

使用 onRemoveTwo 的结果
初始文本 Take = Cariage NOT ME Carriag 将缺失的 voel 添加到 Carriage
要求更正“Cariage”
第一次校正结果 Take = Carriage NOT ME Carriag 将缺失的 voel 添加到 Carriage
With sb = sb.replace(from, to);
要求更正“Carriag”
第二次校正结果 Take = Carriagee NOT MEg 将缺失的 voel 添加到 Carriage
我们有这个错误原因:java.lang.IndexOutOfBoundsException
由我们理解的这行代码引起的
while 正在查找单词的两次出现
txaInput.replaceText(match.start(),match.end(),txtReplacementWord.getText());

使用 onRemove 的结果
初始文本 Take = Cariage NOT ME Carriag 将缺失的 voel 添加到 Carriage
要求更正“Cariage”
第一次校正结果 Take = Carriage NOT ME Carriag 将缺失的 voel 添加到 Carriage
要求更正“Carriag”
第二次修正结果 Take = Carriagee NOT ME Carriage 将缺失的 voel 添加到 Carriagee
请注意,“Carriage”都已更改为“Carriagee”

所以我们的问题是如何更具体地修正要纠正的词?

private void onRemoveTwo(){

    if(txtReplacementWord.getText().isEmpty()){
        txtMessage.setText("No Replacement Word");
        return;
    }
    cboMisspelledWord.getItems().remove(txtWordToReplace.getText());
    // Line Above Removes misspelled word from cboSelect 
    // ==================================================

    String text = txaInput.getText();
    String wordToFind = txtWordToReplace.getText();
    Pattern word = Pattern.compile(wordToFind);
    Matcher match = word.matcher(text);

    while(match.find()){

    ///System.out.println("Found "+word+" "+ match.start() +" - "+ (match.end()-1));

    String from = word.toString();
    String to = txtReplacementWord.getText();
    String sb = txaInput.getText();
    sb = sb.replace(from, to);
    txaInput.replaceText(match.start(),match.end(),txtReplacementWord.getText());

    txtMessage.setText("");
    txtReplacementWord.setText("");
    txtWordToReplace.setText("");
    cboCorrectSpelling.getItems().clear(); 
    cboMisspelledWord.requestFocus();

    // Code above replaces misspelled word with correct spelling in TextArea
    // =====================================================================
    int SIZE = cboMisspelledWord.getItems().size();
    if(SIZE == 0){
        onCheckSpelling();
    }
    }
}

可行的方法,但会更改多个单词

    @FXML
private void onReplace(){

    if(txtReplacementWord.getText().isEmpty()){
        txtMessage.setText("No Replacement Word");
        return;
    }

    cboMisspelledWord.getItems().remove(txtWordToReplace.getText());
    // Line Above Removes misspelled word from cboSelect 
    // ==================================================

    String from = txtWordToReplace.getText();
    String to = txtReplacementWord.getText();
    String sb = txaInput.getText();

    sb = sb.replace(from, to);
    //sb = sb.replaceAll(from,to);
    txaInput.setText("");
    txaInput.setText(sb);
    txtMessage.setText("");
    txtReplacementWord.setText("");
    txtWordToReplace.setText("");
    cboCorrectSpelling.getItems().clear(); 
    cboMisspelledWord.requestFocus();

    // Code above replaces misspelled word with correct spelling in TextArea
    // =====================================================================
    int SIZE = cboMisspelledWord.getItems().size();
    if(SIZE == 0){
        onCheckSpelling();
    }  
}

【问题讨论】:

    标签: javafx textarea stringbuilder


    【解决方案1】:

    好吧@Grendel,我不知道为什么这个问题被否决。我一直在使用 TextArea 进行类似的项目,并且喜欢您的代码,并且您发现 StringBuilder 发现任何出现的字符令人沮丧。所以这是一个答案,代码不是很整洁,你需要清理它。我很不高兴我不得不去一个 String[] 数组然后去一个 ArrayList 将继续处理这个问题
    尽管有反对票,但请享受代码
    将支票发送至 90.83.140.38

    @FXML
    private void onReplace(){
    
        if(txtReplacementWord.getText().isEmpty()){
            txtMessage.setText("No Replacement Word");
            return;
        }
    
        cboMisspelledWord.getItems().remove(txtWordToReplace.getText());
        // Line Above Removes misspelled word from cboMisspelledWord 
        // ==========================================================
        String line = txaInput.getText();
        oneA = line.split("\\s");
        List<String> list = new ArrayList<>(Arrays.asList(oneA));
    
            int theIndex = list.indexOf(txtWordToReplace.getText());
            String gotME = list.get(theIndex);
            list.remove(theIndex);
            list.add(theIndex,txtReplacementWord.getText());
            sb = new StringBuilder(); 
        for (String addWord : list) {
            sb.append(addWord);
            sb.append(" ");
        }
        txaInput.setText(sb.toString()); 
        txtMessage.setText("");
        txtReplacementWord.setText("");
        txtWordToReplace.setText("");
        cboCorrectSpelling.getItems().clear(); 
        cboMisspelledWord.requestFocus();
        // Code above replaces misspelled word with correct spelling in TextArea
        // =====================================================================
        if(cboMisspelledWord.getItems().isEmpty()){
            onCheckSpelling();
        } 
    }
    

    【讨论】:

    • 在@James_Duh 上面的回答中,这行代码 oneA = line.split("\\s");应该是 oneA = line.split(" ");或者您在 TextArea 中丢失格式,它会去掉“\n”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-23
    • 1970-01-01
    • 1970-01-01
    • 2019-03-15
    • 1970-01-01
    相关资源
    最近更新 更多