【发布时间】:2015-12-09 13:47:19
【问题描述】:
我试图在 jtextpane 中突出显示多个单词。但是,我只能突出显示找到的第一个实例。
String key = "are";
Highlighter h = test.outputPane.getHighlighter();
String text = test.outputPane.getText();
String arr[] = text.split("[\\p{Punct}\\s]+");
for (int i = 0; i < arr.length; i++) {
String temp = arr[i];
if (temp.equals(key)) {
try {
h.addHighlight(text.indexOf(temp), text.indexOf(temp) + temp.length(), DefaultHighlighter.DefaultPainter);
} catch (BadLocationException ex) {
Logger.getLogger(crawler.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
【问题讨论】:
-
嗨。请提供MCVE。这段代码不足以重现问题。