私はこのコードを持っています:
JTextArea textComp;
Highlighter hilite = textComp.getHighlighter();
if (word.toString().equals(pattern[i]))
{
hilite.addHighlight(posStart, (posEnd), myHighlighter);
break;
}
word
ですStringBuilder
andの条件がif
一致するとhilite.addHighlight(posStart, (posEnd), myHighlighter);
します。このステートメントが実行されます。次に、textComp
含まれている
int myVar
そして、私はこのように強調しようとします
int myVar
その時、posStart = 0
そしてposEnd = 3
。しかしtextArea
、ハイライターに何かを入力していると、次のように最後まで拡張されます。
int myVar
誰でもこれで私を助けることができますか?
そして、私が声明を出すと:
hilite.addHighlight(posStart, (posEnd-1), myHighlighter);
次に でposStart=0, posEnd=3
、その後のみ
* *t myVarでこれが発生します。つまり、"in" は強調表示されていますが、"t" は強調表示されていません。
編集 機能:
Highlighter.HighlightPainter myHighlighter = new MyHighlightPainter(
Color.LIGHT_GRAY);
try {
Highlighter hilite = textComp.getHighlighter();
Document doc = textComp.getDocument();
String text = doc.getText(0, doc.getLength());
String[] words = text.split(" ");
int posEnd, posStart = 0;
while (text.length() > posStart) {
posEnd = posStart;
StringBuilder word = new StringBuilder();
while (posEnd < text.length() && text.charAt(posEnd) != ' ') {
word.append(text.charAt(posEnd++));
}
for (int i = 0; i < pattern.length; i++) {
if (word.toString().equals(pattern[i])) {
hilite.addHighlight(posStart, (posEnd-1), myHighlighter);
break;
}
}
posStart = posStart + posEnd + 1;
}
} catch (BadLocationException e) {
}