2016-03-19 7 views
1

特定の単語や記号の色やフォントが異なるテキストエディタを含むJTextPaneを作成しようとしています。 これまでのところ私はこのように、言葉に色を変更する方法を見つける: - 変更する必要があります「>」JTextPane内の特定のシンボルに色を変更する方法[java]

import javax.swing.*; 
import java.awt.*; 
import javax.swing.text.*; 

public class Test extends JFrame { 

    private int findLastNonWordChar(String text, int index) { 
     while (--index >= 0) { 
      if (String.valueOf(text.charAt(index)).matches("\\W")) { 
       break; 
      } 
     } 
     return index; 
    } 

    private int findFirstNonWordChar(String text, int index) { 
     while (index < text.length()) { 
      if (String.valueOf(text.charAt(index)).matches("\\W")) { 
       break; 
      } 
      index++; 
     } 
     return index; 
    } 

    public Test() { 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     setSize(400, 400); 
     setLocationRelativeTo(null); 

     final StyleContext cont = StyleContext.getDefaultStyleContext(); 
     final AttributeSet attr = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.RED); 
     final AttributeSet attrBlack = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.BLACK); 
     DefaultStyledDocument doc = new DefaultStyledDocument() { 
      private static final long serialVersionUID = 1L; 

      @Override 
      public void insertString(int offset, String str, AttributeSet a) throws BadLocationException { 
       super.insertString(offset, str, a); 

       String text = getText(0, getLength()); 
       int before = findLastNonWordChar(text, offset); 
       if (before < 0) { 
        before = 0; 
       } 
       int after = findFirstNonWordChar(text, offset + str.length()); 
       int wordL = before; 
       int wordR = before; 

       while (wordR <= after) { 
        if (wordR == after || String.valueOf(text.charAt(wordR)).matches("\\W")) { 
         if (text.substring(wordL, wordR).matches("(\\W)*(private|public|protected)")) { 
          setCharacterAttributes(wordL, wordR - wordL, attr, false); 
         } else { 
          setCharacterAttributes(wordL, wordR - wordL, attrBlack, false); 
         } 
         wordL = wordR; 
        } 
        wordR++; 
       } 
      } 

     }; 
    } 

    public static void main(String args[]) { 
     new Test(); 
    } 
} 

私の問題は、ときのシンボルのカラーです。実際には、「私的」または「公的」の代わりに「 - >」を入れれば、それはうまくいかない。 これを行う方法を見つけるのを手伝ってもらえますか?おかげ

答えて

0

チェック(前回の記事からピックアップ)このサンプル:

public static void main(String[] args) { 
    JTextPane jtp = new JTextPane(); 
    StyledDocument doc = jtp.getStyledDocument(); 

    Style style = jtp.addStyle("Red coloured text", null); 
    StyleConstants.setForeground(style, Color.red); 

    try { doc.insertString(doc.getLength(), "Style text",style); } 
    catch (BadLocationException e){} 

    StyleConstants.setForeground(style, Color.blue); 

    try { doc.insertString(doc.getLength(), "Style text 2",style); } 
    catch (BadLocationException e){} 

    JFrame frame = new JFrame("ColourCheck"); 
    frame.getContentPane().add(jtp); 
    frame.pack(); 
    frame.setVisible(true); 
}