我正在尝试将ScrollBar位置移动到TextArea的单词。 那是代码:
class MyHighlightPainter extends DefaultHighlighter.DefaultHighlightPainter {
public MyHighlightPainter (Color color) {
super(color);
}
} Highlighter.HighlightPainter myHighlightPainter = new MyHighlightPainter(Color.red);
public void removeHighlights(JTextComponent textComp) {
Highlighter hilite = textComp.getHighlighter();
Highlighter.Highlight[] hilites = hilite.getHighlights();
for (int i=0; i<hilites.length; i++) {
if (hilites[i].getPainter() instanceof MyHighlightPainter) {
hilite.removeHighlight(hilites[i]);
}
}
}
public void highligh(JTextComponent textComp,String pattern){
removeHighlights(textComp);
try{
Highlighter hilite = textComp.getHighlighter();
Document doc = textComp.getDocument();
String text = doc.getText(0, doc.getLength());
int pos = 0;
while((pos=text.toUpperCase().indexOf(pattern.toUpperCase(),pos))>=0){
hilite.addHighlight(pos, pos+pattern.length(), myHighlightPainter);
pos +=pattern.length();
spCodes.getVerticalScrollBar().setValue(pos);
}
}catch(Exception e){
}
}
但是当我使用“ pos”设置滚动条的值时,当我启动程序并搜索某些内容时滚动条比我一直在寻找的单词要低 ,没有立即看到! 我说的是这个
spCodes.getVerticalScrollBar().setValue(pos);
PS:spCodes是我的滚动面板