【发布时间】:2017-05-16 02:26:53
【问题描述】:
在JEditorPane中,我想搜索一个字符串,我知道scrollToReference不会起作用,所以我想知道如何遍历Pane并找到一个指定的字符串。
如何遍历 JEditorPane 以在 java 中找到指定的字符串?
【问题讨论】:
标签: java swing document jeditorpane
在JEditorPane中,我想搜索一个字符串,我知道scrollToReference不会起作用,所以我想知道如何遍历Pane并找到一个指定的字符串。
如何遍历 JEditorPane 以在 java 中找到指定的字符串?
【问题讨论】:
标签: java swing document jeditorpane
我能想到的最简单的方法就是浏览文档。
Document document = editor.getDocument();
try {
String find = //... String to find
for (int index = 0; index + find.length() < document.getLength(); index++) {
String match = document.getText(index, find.length());
if (find.equals(match)) {
// You found me
}
}
} catch (BadLocationException ex) {
ex.printStackTrace();
}
您可以根据这个概念编写一些例程来“找到下一个单词”
您可能还会发现Highlight a word in JEditorPane 和Java - Scroll to specific text inside JTextArea 都使用相似的方法来实现不同的目标