2012-09-18 31 views
3

我在一個框架打開一個文件打開一個文本文件的話,我想強調幾個我明白words.As的,我需要遍歷文件的內容。如何遍歷內容以及我可能用於突出顯示的相關屬性?突出的幾幀中的


UPDATE:我的代碼是這樣的


private JEditorPane editorpane; 
JScrollPane editorScrollPane; 

public TextEditor() 
{ 
    editorpane = new JEditorPane(); 
    editorpane.setEditable(false); 

    if (filename != null) 
    { 
     try 
     { 
      File file = new File(filename); 
      editorpane.setPage(file.toURI().toURL()); 
     } 
     catch (IOException e) 
     { 
      e.printStackTrace(); 
      System.err.println("Attempted to read a bad file ..."); 
     } 
    } 
    else 
    { 
     System.err.println("File name is wrong"); 
    } 

    add(editorpane); 
} 
+1

這個問題的答案有幫助嗎? http://stackoverflow.com/questions/12477579/opening-a-text-file-in-a-frame-using-swing-components如果是這樣,請接受一個。你如何在一個框架中打開一個文件? –

回答

6

要突出一個JEditorPane看看這個例子:

enter image description here

import java.awt.Color; 
import javax.swing.JEditorPane; 
import javax.swing.JFrame; 
import javax.swing.SwingUtilities; 
import javax.swing.text.BadLocationException; 
import javax.swing.text.DefaultHighlighter; 
import javax.swing.text.Document; 
import javax.swing.text.Highlighter; 
import javax.swing.text.JTextComponent; 

public class Test { 

    // An instance of the subclass of the default highlight painter 
    static MyHighlightPainter myHighlightPainter = new MyHighlightPainter(Color.red); 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       JFrame frame = new JFrame(); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

       JEditorPane jep = new JEditorPane(); 
       jep.setText("Hello to the public"); 
       frame.add(jep); 
       frame.pack(); 
       frame.setVisible(true); 

       highlight(jep, "public");//'public is the word to highligh' 

      } 
     }); 
    } 

    // Creates highlights around all occurrences of pattern in textComp 
    public static void highlight(JTextComponent textComp, String pattern) { 
     // First remove all old highlights 
     removeHighlights(textComp); 

     try { 
      Highlighter hilite = textComp.getHighlighter(); 
      Document doc = textComp.getDocument(); 
      String text = doc.getText(0, doc.getLength()); 
      int pos = 0; 

      // Search for pattern 
      while ((pos = text.indexOf(pattern, pos)) >= 0) { 
       // Create highlighter using private painter and apply around pattern 
       hilite.addHighlight(pos, pos + pattern.length(), myHighlightPainter); 
       pos += pattern.length(); 
      } 
     } catch (BadLocationException e) { 
     } 
    } 

    // Removes only our private highlights 
    public static void removeHighlights(JTextComponent textComp) { 
     Highlighter hilite = textComp.getHighlighter(); 
     Highlighter.Highlight[] hilites = hilite.getHighlights(); 

     for (int i = 0; i < hilites.length; i++) { 
      if (hilites[i].getPainter() instanceof MyHighlightPainter) { 
       hilite.removeHighlight(hilites[i]); 
      } 
     } 
    } 
} 

class MyHighlightPainter extends DefaultHighlighter.DefaultHighlightPainter { 

    public MyHighlightPainter(Color color) { 
     super(color); 
    } 
} 
+1

非常正確的方法來刪除所有熒光筆 – mKorbel