我需要能够突出两个字符中的单词。例如,:
//Highlight whatever is in between the two quotation marks
String keyChars = " \" \" ";
几个星期以来,我一直在为此苦苦挣扎。我查了一下,阅读了源代码,编写了代码,但我还没有找到能够做到这一点的方法。
答案 0 :(得分:1)
以下代码段有效。
ed=new JEditorPane();
ed.setText("This \"text\" contains \"quotes\". The \"contents\" of which are highlighted");
Pattern pl;
pl=Pattern.compile("\"");
Matcher matcher = pl.matcher(ed.getText());
int end=0,beg=0;
while(matcher.find())
{
beg=matcher.start();
matcher.find(); //finding the next quote
end=matcher.start();
DefaultHighlightPainter d = new DefaultHighlightPainter(Color.YELLOW);
try {
ed.getHighlighter().addHighlight(beg+1, end,d);
} catch (BadLocationException ex) {
ex.printStackTrace();
}
}