按键回车搜索单词



我的搜索方法有问题。

使用此方法,我可以在文本字段中输入一个单词并在文本区域中显示该单词。但是,如果我让它运行,这只会发生一次。我需要扩展它,以便每次我单击"输入"时,程序都应该继续在文本区域中搜索。我该怎么做?请给我代码示例。我的演讲只剩下 2 天了。

非常感谢您的帮助

textfield.setOnKeyPressed(new EventHandler<KeyEvent>() {
        @Override
        public void handle(KeyEvent event) {
            if (event.getCode() == KeyCode.ENTER)  {
                String text = textarea.getText();
              Labeled errorText = null;
            if (textfield.getText() != null && !textfield.getText().isEmpty()) {
                    index = textarea.getText().indexOf(textfield.getText()); 
                    textarea.getText();
                    if (index == -1) {
                        errorText.setText("Search key Not in the text");
                    } else {
                      //  errorText.setText("Found");
                        textarea.selectRange(index, index + textfield.getLength());
                        }

                    }   
                }
            }
        });

indexOf方法有一个重载版本,允许您从特定索引开始搜索。跟踪上次查找的索引,然后从此位置开始搜索:

@Override
public void start(Stage primaryStage) throws Exception {
    TextField textField = new TextField("foo");
    TextArea textarea = new TextArea();
    for (int i = 0; i < 10; i++) {
        textarea.appendText("foonbarfoobarfoofoon");
    }
    textField.setOnAction(evt -> {
        String searchText = textField.getText();
        if (searchText.isEmpty()) {
            return; // searching for empty text doesn't make sense
        }
        int index = textarea.getSelection().getEnd();
        // in case of the first search, start at the beginning
        // TODO: adjust condition/starting index according to needs
        if (textarea.getSelection().getLength() == 0) {
            index = 0;
        }
        // find next occurrence
        int newStartIndex = textarea.getText().indexOf(searchText, index);
        // mark occurrence
        if (newStartIndex >= 0) {
            textarea.selectRange(newStartIndex, newStartIndex + searchText.length());
        }
    });
    Scene scene = new Scene(new VBox(textField, textarea));
    primaryStage.setScene(scene);
    primaryStage.show();
}

编辑

如果您对在选择后(或光标之后,如果没有选择范围)搜索元素不满意,则可以保存最后一个匹配项结束时的数据:

@Override
public void start(Stage primaryStage) throws Exception {
    TextField textField = new TextField("foo");
    TextArea textarea = new TextArea();
    for (int i = 0; i < 10; i++) {
        textarea.appendText("foonbarfoobarfoofoon");
    }
    class SearchHandler implements EventHandler<ActionEvent> {
        int index = 0;
        @Override
        public void handle(ActionEvent event) {
            String searchText = textField.getText();
            String fullText = textarea.getText();
            if (index + searchText.length() > fullText.length()) {
                // no more matches possible
                // TODO: notify user
                return;
            }
            // find next occurrence
            int newStartIndex = textarea.getText().indexOf(searchText, index);
            // mark occurrence
            if (newStartIndex >= 0) {
                index = newStartIndex + searchText.length();
                textarea.selectRange(newStartIndex, index);
            } else {
                index = fullText.length();
                // TODO: notify user
            }
        }
    }
    SearchHandler handler = new SearchHandler();
    textField.setOnAction(handler);
    // reset index to search from start when changing the text of the TextField
    textField.textProperty().addListener((o, oldValue, newValue) -> handler.index = 0);
    Scene scene = new Scene(new VBox(textField, textarea));
    primaryStage.setScene(scene);
    primaryStage.show();
}

最新更新