javafx 8 编辑文本区域中的文本

javafx 8 edit text in textarea

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

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

使用 onRemove 的结果
初始文本 Take = Cariage NOT ME Carriag add missing voel to Carriage
要求更正 "Cariage"
第一次校正结果 Take = Carriage NOT ME Carriag add missing voel to Carriage
要求更正 "Carriag"
第二次修正结果 Take = Carriagee NOT ME Carriage add missing voel to 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();
    }  
}

好吧@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();
    } 
}