2013-09-22 2 views
0

현재 텍스트 편집기를 사용하고 있으며 특정 단어의 배경을 다른 것과 다르게 만드는이 코드가 있지만 전경에서 배경이 아닌 색을 편집하고 싶습니다. 여기 JEditorPane은 다른 단어에 대한 전경색을 설정합니다.

는 내가 가지고있는 코드입니다 : ...

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JEditorPane; 
import javax.swing.JFrame; 
import javax.swing.JTextPane; 
import javax.swing.text.AbstractDocument; 
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; 
import javax.swing.text.MutableAttributeSet; 
import javax.swing.text.SimpleAttributeSet; 
import javax.swing.text.StyleConstants; 
public class test { 
    final static JEditorPane jep = new JEditorPane(); 
    static JButton button = new JButton("Refresh"); 
    static JTextPane jTextPane1 = new JTextPane(); 
    public static void main(String[] args) throws BadLocationException { 
       JFrame frame = new JFrame(); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

       jep.setText("Hello to the public place here are the public people"); 
       frame.add(jep); 
       frame.pack(); 
       frame.setSize(500, 500); 
       frame.add(button,BorderLayout.NORTH); 
       frame.setVisible(true); 
       button.addActionListener(new ActionListener(){ 
        public void actionPerformed(ActionEvent arg0) { 
          try { 
           highlight(jep, "public"); 
          } catch (BadLocationException e) { 
           // TODO Auto-generated catch block 
           e.printStackTrace(); 
          } 
        } 
       }); 
       highlight(jep, "public"); 
    } 
    public static void highlight(JTextComponent textComp, String pattern) throws BadLocationException { 
     try { 

      final MyHighlightPainter myHighlightPainter = new MyHighlightPainter(Color.red); 
      Highlighter hilite = textComp.getHighlighter(); 
      MutableAttributeSet mas = (MutableAttributeSet)new SimpleAttributeSet(); 
      StyleConstants.setForeground(mas, Color.red); 

      Document doc = textComp.getDocument(); 
      String text = doc.getText(0, doc.getLength()); 
      int pos = 0; 

      // Search for pattern 
      while ((pos = text.indexOf(pattern, pos)) >= 0) { 
       hilite.addHighlight(pos, pos + pattern.length(), myHighlightPainter); 
       pos += pattern.length(); 

      } 
     }finally{ 

     } 
    } 
} 
class MyHighlightPainter extends DefaultHighlighter.DefaultHighlightPainter { 
    public MyHighlightPainter(Obect object) { 
     super((Color) object); 

    } 
} 
+0

import java.awt.Color; import javax.swing.JTextPane; import javax.swing.text.BadLocationException; import javax.swing.text.Element; import javax.swing.text.Style; import javax.swing.text.StyleConstants; import javax.swing.text.StyledDocument; public class Srap { public static void main(String[] args) { JTextPane textPane = new JTextPane(); StyledDocument doc = textPane.getStyledDocument(); Style style = textPane.addStyle("I'm a Style", null); StyleConstants.setForeground(style, Color.red); try { doc.insertString(doc.getLength(), "BLAH ", style); } catch (BadLocationException ex) { } StyleConstants.setForeground(style, Color.blue); try { doc.insertString(doc.getLength(), "BLEH", style); } catch (BadLocationException e) { } } } 
튜토리얼에서 보여주는 것처럼 하이 라이터보다는 오히려. –

답변

3

당신은 간단한 예를 들어, 편집자 StyledDocuemnt을 활용할 필요가

색 세트 속성과 나는 스타일을 사용하려고 할
+0

StyleConstants를 사용해야한다는 것을 이해하지만 relleay를 이해하지 못했습니다. 코드에서 방금 "BLAH"& "BLEH"를 다른 색상으로 "붙여 넣기"했습니다. 그래서 마지막으로 사용한 색을 입력하면 단어를 표시합니다. 아직 사용중인 사람이 "공개"로 표시하고 스타일 상수를 사용하고 "공용"이라는 단어 만 표시하는 프로그램을 만드는 방법을 알고 있다면 나는 웹 연구를 해봤지만 얻을 방법을 모르겠다. 작동하려면 ... – user1886564

+0

https://code.google.com/p/java-syntax-highlighter/ 또는 http://stackoverflow.com/questions/1853419/syntax-highlighter-for-java – MadProgrammer

+0

과 같은 것을 의미합니다. 예 고맙습니다. D – user1886564