【问题标题】:Find a word jtextfield in java notepad在java记事本中查找单词jtextfield
【发布时间】:2013-03-06 05:20:50
【问题描述】:

我应该设置什么条件才能突出显示 JTextArea 中的所有单词? 此代码在没有 while 循环的情况下工作,但它只查找并突出显示第一个单词匹配。

String findstr = findTextField.getText().toUpperCase(); // User Input Word to find
int findstrLength = findstr.length();                   
String findtextarea = textarea.getText().toUpperCase(); // TextArea Content
Highlighter h = textarea.getHighlighter();
h.removeAllHighlights();
try
    {
        int index=0;
        while(index>=0)                             // What should I put here ??
        {
            index = findtextarea.indexOf(findstr,index);
            h.addHighlight(index,index+findstrLength, DefaultHighlighter.DefaultPainter);
        }
    }

【问题讨论】:

    标签: java swing jtextfield jtextcomponent text-comparison


    【解决方案1】:
        while(index>=0) {
            index = findtextarea.indexOf(findstr,index);
            if (index > 0) {
               h.addHighlight(index,index+findstrLength, DefaultHighlighter.DefaultPainter);
            }
            index++; // try adding this to allow you to look for the next index.
        }
    

    【讨论】:

    • 是的!成功了.. 凌晨 4 点,我想了一个小时......没有意识到.. !!我太傻了。谢谢!!!
    • @Sharad Tank Oracle 教程中的可编译示例
    猜你喜欢
    • 2016-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-01
    • 1970-01-01
    • 2013-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多