【问题标题】:Scanning through a jTextArea for a word通过 jTextArea 扫描一个单词
【发布时间】:2014-05-06 19:07:37
【问题描述】:

我正在尝试创建一个搜索栏,以突出显示文本区域中的相应单词。我遇到的问题是下面的代码示例仅突出显示文本区域中第一次出现的单词,即它不会扫描整个文本区域。如何使关键字的所有出现都突出显示?

public void keywordSearch() {
    hilit.removeAllHighlights();
    String keyword = txtSearch.getText();
    String content = txtArea.getText();
    int index = content.indexOf(keyword, 0);
    if (index >= 0) {  // if the keyword was found
        try {

            int end = index + keyword.length();
            hilit.addHighlight(index, end, painter);
            txtSearch.setBackground(Color.WHITE);

        } catch (BadLocationException e) {
            e.printStackTrace();
        }
    } else {
        txtSearch.setBackground(ERROR_COLOR);// changes the color of the text field if the keyword does not exist
    }
}

我已尝试使用 Scanner 类进行以下修复,但仍然无法正常工作。

Scanner sc = new Scanner(content);

    if (index >= 0) {  // if the keyword was found
        try {
            while(sc.hasNext() == true)
            {
                int end = index + keyword.length();
                hilit.addHighlight(index, end, painter);
                txtSearch.setBackground(Color.WHITE);
                sc.next();
            }

非常感谢任何帮助。提前致谢。

使用while循环修复(进入无限循环)

    while(index >= 0) {   // if the keyword is found
        try {
            int end = index + keyword.length();
            hilit.addHighlight(index, end, painter);
            txtSearch.setBackground(Color.WHITE);
            index = content.indexOf(keyword, index);
            System.out.println("loop");// test to see if entered infinite loop

        } catch (BadLocationException e) {
            e.printStackTrace();
        }
    }

【问题讨论】:

    标签: java java.util.scanner jtextfield jtextarea


    【解决方案1】:

    关键在这里:

    int index = content.indexOf(keyword, 0);
    if (index >= 0) {  // if the keyword was found
    

    将此更改为从第一次找到的索引再次搜索的while循环:

    int index = content.indexOf(keyword, 0);
    while (index >= 0) {  // if the keyword was found
        // Do stuff
        index = content.indexOf(keyword, index);
    }
    

    您还需要更改您的 final else 以再次检查它是否存在(有几种方法可以做到这一点)。

    【讨论】:

    • 试过这个,但它进入了一个无限循环。
    • @user3469429 您可能需要从第二个索引+1 开始扫描。
    【解决方案2】:

    您只查找单词的出现次数。

    ...
    for(int i = 0; i < content.length(); i++){
    
      int index = content.indexOf(keyword, i);
      if (index >= 0) {  // if the keyword was found
    
        int end = index + keyword.
        hilit.addHighlight(index, end, painter);
        txtSearch.setBackground(Color.WHITE);
    
      }
    ...
    

    这个 for 循环将搜索整个内容字符串并将所有出现的关键字发送到 addHighlight 方法。

    【讨论】:

    • 呃。不要这样做。它会“工作”,因为它确实会突出显示每个单词,但它的效率非常低,因为您会一遍又一遍地反复查找并突出显示同一个单词。
    【解决方案3】:
    ...
    ArrayList<Integer> keywordIndexes = new ArrayList<Integer>();
    int index = content.indexOf(keyword, 0);
    for(int i = 0; i < content.length(); i++){
        if (index >= 0) { // if keyword found
    
        int end = index + keyword.length();
        keywordIndexes.add(index); // Add index to arraylist
        keywordIndexes.add(end);  // Add end to next slot in arraylist
    }
    
    for(int j = 0; j < keywordIndexes.size(); j+=2){
        hilit.addHighlight(j, (j+1), painter);
        txtSearch.setBackground(Color.WHITE);
    }
    ...
    

    这可能不是最优化的代码,但它应该可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 2012-03-27
      • 2014-02-14
      • 1970-01-01
      • 2015-01-18
      • 2010-11-14
      • 1970-01-01
      相关资源
      最近更新 更多