Java 如何在Textarea中搜索单词

Java 如何在Textarea中搜索单词,java,search,jtextarea,Java,Search,Jtextarea,我想突出显示从插入符号位置到文本区域结尾的单词,然后突出显示从文本区域开始到结尾的单词,然后以循环方式重复此过程。我的代码仅适用于当前插入符号位置到文本区域结尾的情况。请检查一次 我的代码: public void highLightWholeWord(JTextArea component, String patteren) { try { Document doc = component.getDocument(); String text = component.getTe

我想突出显示从插入符号位置到文本区域结尾的单词,然后突出显示从文本区域开始到结尾的单词,然后以循环方式重复此过程。我的代码仅适用于当前插入符号位置到文本区域结尾的情况。请检查一次

我的代码:

public void highLightWholeWord(JTextArea component, String patteren) {
try {
    Document doc = component.getDocument();
    String text = component.getText(0, doc.getLength());
    int pos = component.getCaretPosition();
    boolean found = false;
    int findLength = patteren.length();
    // Rest the search position if we're at the end of the document
    if (pos + findLength > doc.getLength()) {
        pos = 0;
    }
    while (pos + findLength <= doc.getLength()) {
        // Extract the text from teh docuemnt
        String match = doc.getText(pos, findLength).toLowerCase();
        // Check to see if it matches or request
        if (match.equals(patteren)) {
            if (pos - 1 >= 0
                        && Character.isWhitespace(doc.getText(pos - 1, 1).charAt(0))
                                        || Character.isWhitespace(doc.getText(findLength, 1).charAt(0))) {
                if (pos + findLength == doc.getLength()
                                             || Character.isWhitespace(doc.getText(pos + findLength, 1).charAt(0))) {
                    found = true;
                    break;
                }
            }
        }
        pos++;
    }
    if (found) {
    component.setSelectionStart(pos);
    component.setSelectionEnd(pos + patteren.length());
    component.getCaret().setSelectionVisible(true);
    }
} 
catch (Exception e) {
    e.printStackTrace();
}
}
public void highLightWholeWord(JTextArea组件,字符串模式){
试一试{
Document doc=component.getDocument();
String text=component.getText(0,doc.getLength());
int pos=component.getCaretPosition();
布尔值=false;
int findlelength=patteren.length();
//如果我们在文档的末尾,请停止搜索位置
如果(pos+findLength>doc.getLength()){
pos=0;
}
而(pos+findLength=0
&&Character.isWhitespace(doc.getText(pos-1,1).charAt(0))
||Character.isWhitespace(doc.getText(findleength,1.charAt(0))){
如果(pos+findLength==doc.getLength()
||Character.isWhitespace(doc.getText(pos+findlelength,1).charAt(0))){
发现=真;
打破
}
}
}
pos++;
}
如果(找到){
组件设置选择启动(pos);
组件.setSelectionEnd(pos+patteren.length());
component.getCaret().setSelectionVisible(true);
}
} 
捕获(例外e){
e、 printStackTrace();
}
}

让我们看一个文本示例,假设我们正在搜索单词“xxx”。假设上次单击按钮时,它选择了最后一个xxx:

xxx ttt aaa xxx fff xxx yyyxxxzzz

此文本的大小为35个字符

现在,插入符号位置位于突出显示的xxx(位置31)的末尾。图案长度为3。您的if条件是:

if (pos + findLength > doc.getLength()) {
    pos = 0;
}
现在,
pos+findlelength
31+3
,这是
34
。因此它不大于
doc.getLength()
。因此它不会设置
pos=0

因此,您将从位置31开始搜索
xxx
,但是那里没有更多的
xxx
。只要
zzz
。最终,您将到达文档的末尾,并且不会设置
found
。因此选择不会改变,插入符号将保持在位置31

因此,无论你按下按钮多少次,同样的事情都会发生

这意味着您设置
pos=0
的条件是错误的。只有当xxx绝对是最后一个字时,它才会起作用

以下是解决这一问题的建议:

用另一种方法定义您的条件:

public boolean foundPattern( Document doc, int pos, int findLength  ) {
    String match = doc.getText(pos, findLength).toLowerCase();
    // Check to see if it matches or request
    if (match.equals(patteren)) {
        if (pos - 1 >= 0
                    && Character.isWhitespace(doc.getText(pos - 1, 1).charAt(0))
                                    || Character.isWhitespace(doc.getText(findLength, 1).charAt(0))) {
            if (pos + findLength == doc.getLength()
                                         || Character.isWhitespace(doc.getText(pos + findLength, 1).charAt(0))) {
                return true;
            }
        }
    }
    return false;
}
这只是为了使编写实际while部分更容易:

while (pos + findLength <= doc.getLength()) {

   found = foundPattern( doc, pos, findLength );

   if ( found ) {
      break;
   }

   pos++;

}

if ( ! found ) {
   pos = 0;

   while ( pos + findLength < component.getCaretPosition() ) {
       found = foundPattern( doc, pos, findLength );
       if ( found ) {
          break;
       }

       pos++;
   }
}

while(pos+findlelength)不清楚您想做什么。您可以编辑您的问题并显示文本示例以及希望突出显示的位置(您可以使用粗体来模拟选择)。如果(find)循环突出显示模式词。我的代码突出显示当前插入符号位置(courser位置)的搜索词但,我的要求是从插入符号位置到文本区域的末尾,然后从文本区域的开始到结束,然后继续以这种方式(循环方式)搜索单词。您的代码只突出显示一个单词。它将突出显示到单词的结尾,而不是文本区域的结尾。我看不到任何突出显示多个单词的内容。因此缺少一些代码。请编辑您的问题并添加它。是的,一次突出显示一个单词。当我单击FindNext按钮时,它将突出显示下一个出现的单词搜索单词。以这种方式直到文本区域结束。但是,我希望在结束后从开始到结束突出显示。它工作正常。非常感谢。