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

问题描述 投票:-1回答:1

我们正在尝试纠正TextArea中单词的拼写我们已经尝试了两个方法onRemoveTwo失败,出现IndexOutOfBoundsExceptiononRemove的另一种方法可以用,但是它可以执行replaceAll我们在代码中使用replace这是两种方法的结果

使用onRemoveTwo的结果初始文字Take = Cariage NOT ME Carriag将缺失的龙骨添加到Carriage请求是要更正“ Cariage”第一个更正结果取=马车不是我的Carriag将缺少的龙骨添加到Carriage随着sb = sb.replace(from,to);要求更正“” Carriag“第二次更正结果乘以=马车不是MEg将遗漏的语音添加到马车中我们遇到此错误的原因:java.lang.IndexOutOfBoundsException由我们了解的这一行代码引起而正在发现单词的两个出现txaInput.replaceText(match.start(),match.end(),txtReplacementWord.getText());

使用onRemove的结果初始文字Take = Cariage NOT ME Carriag将缺失的龙骨添加到Carriage请求是要更正“ Cariage”第一个更正结果取=马车不是我的Carriag将缺少的龙骨添加到Carriage要求更正“” Carriag“第二校正结果Take = Carriagee NOT ME 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个回答
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();
    } 
}
© www.soinside.com 2019 - 2024. All rights reserved.