我编写了一个代码,用于在 TextArea 中查找特定单词并突出显示它。但是,它搜索并突出显示从插入符位置到 TextArea 末尾的单词。但是,我想搜索从插入符位置到末尾的单词TextArea然后从Textarea的开始到结束再从开始到结束(循环方式)。我在这里粘贴了我的搜索方法。请检查它。谢谢。
我的搜索词方法:
public void highLight(JTextArea component,String patteren){
try {
Document doc=component.getDocument();
String text=component.getText(0,doc.getLength());
int pos=component.getCaretPosition();
int index=text.toLowerCase().indexOf(patteren.toLowerCase(),pos);
if (index>=0) {
component.setSelectionStart(index);
component.setSelectionEnd(index+patteren.length());
component.getCaret().setSelectionVisible(true);
}
}
catch(Exception e){
}
}
最佳答案
int index=text.toLowerCase().indexOf(patteren.toLowerCase(),pos);
if (index>=0) {
component.setSelectionStart(index);
component.setSelectionEnd(index+patteren.length());
component.getCaret().setSelectionVisible(true);
}
else {
index=text.substring(0,pos).toLowerCase().indexOf(patteren.toLowerCase());
if (index>=0) {
component.setSelectionStart(index);
component.setSelectionEnd(index+patteren.length());
component.getCaret().setSelectionVisible(true);
}
}
如果没有找到任何内容,请使用文本的前一部分
关于java - 如何以循环方式突出显示 TextArea 中的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26903292/