publicvoidcolor(){inti,f;Stringmad="";Stringh=jTextArea1.getText();i=jTextField1.getText().length();Highlighterhilite=jTextArea1.getHighlighter();//获取担任进行高亮显示的对象//Highlighter:一个允许用户用带有颜色的区域标出背景的对象接口for(f=0;f<h.length();f++){if(i<h.length()){mad=h.substring(f,i++).toString();//System.out.println(str);}if(jTextField1.getText().compareToIgnoreCase(mad)==0){try{intg=jTextField1.getText().length();//向视图添加高亮显示hilite.addHighlight(f,f+g,newMyHighlightPainter(Color.PINK));//jTextArea1.select(f,f+i);//jTextArea1.setSelectionColor(Color.RED);//System.out.println("成功了!");}catch(BadLocationExceptionex){Logger.getLogger(newsos.class.getName()).log(Level.SEVERE,null,ex);}}}}
1