【发布时间】:2019-05-24 08:45:43
【问题描述】:
我的搜索方法有问题。
通过这种方法,我可以在文本字段中输入一个单词,并在文本区域中显示该单词。但是,如果我让它运行,这只发生一次。我需要展开它,这样每次我点击“enter”时,程序都应该继续在 textarea 中搜索。我怎样才能做到这一点? 请给我代码示例。我的演讲只剩下 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());
}
}
}
}
});
【问题讨论】:
-
请提供一个minimal reproducible example 来说明问题。