2016-06-11 5 views
0

텍스트를 쓸 때 원하는 색상을 지정하는 방법이 있습니까? "Apple"을 빨간색으로 설정했다고합시다. JTextPane에 "이 Apple은 좋은 Apple입니다"라고 쓰면 "사과"라는 단어가 빨간색으로 바뀌어야합니다.JTextPane에서 특정 단어를 쓸 때마다 텍스트 색상 지정

여기까지 내가 지금까지 가지고있는 것이지만, 모두 검은 색으로 표시됩니다. Apple을 빨간색으로 표시하고 싶습니다.

public static void main(String[] args) { 
    JTextPane textPane = new JTextPane(); 
    //Would like to make the words Apple Red in foreground color 
    textPane.setText("this Apple is good Apple"); 


    JFrame frame = new JFrame("Test"); 
    frame.getContentPane().add(textPane); 
    frame.pack(); 
    frame.setVisible(true); 
} 

답변

0

다음은 색상에 대한 단어 맵을 사용하는 솔루션입니다. 당신이 볼 수 있듯이, 나는 빨간색과 감사

public class Test { 

    HashMap<String, Color> myMap = new HashMap<String, Color>(); 

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

    public Test() { 
     myMap.put("apple", Color.RED); 
     myMap.put("apples", Color.RED); 
     myMap.put("green", Color.GREEN); 
     String text = "This is a green apple and I like to eat Apples"; 

     JTextPane textPane = new JTextPane(); 
     StyledDocument doc = textPane.getStyledDocument(); 



     Style style = textPane.addStyle("Red Style", null); 
     StyleConstants.setForeground(style, Color.red); 
     ArrayList<Chunk> chunks = getColorsBasedOnText(text, textPane); 
     try { 
      for (Chunk chunk : chunks) { 
       doc.insertString(doc.getLength(), chunk.text + " ", chunk.style); 
      } 
     } catch (BadLocationException e) { 
     } 

     JFrame frame = new JFrame("Test"); 
     frame.getContentPane().add(textPane); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    private ArrayList<Chunk> getColorsBasedOnText(String text, JTextPane textPane) { 
     ArrayList<Chunk> chunks = new ArrayList<Chunk>(); 
     for (String word: text.split(" ")) { 
      Chunk chunk = new Chunk(); 
      chunk.text = word; 
      Color color = myMap.get(word.toLowerCase()); 
      if (color != null) { 
       chunk.style = textPane.addStyle("Style", null); 
       StyleConstants.setForeground(chunk.style, color); 
      } 
      chunks.add(chunk); 
     } 
     return chunks; 
    } 

    private class Chunk { 
     public String text; 
     public Style style; 
    } 
+0

JTextPane GUI에서 단어를 쓸 때 단어 색상을 변경하는 방법이 있습니까? 프로그램을 실행하고 "녹색"이라는 단어를 다시 쓰면 녹색으로 변하지 않기 때문입니다. 그것은 실제로 내가하려고하는 것입니다. 미안하지만 내가 더 잘 설명하지 못한다면. –

+0

JTextPane을 만든 후에 스타일을 지정할 수 있어야합니다. 예제를 짧고 다정하게 유지하기 위해 한 번에 모든 것이 있습니다. 프레임을 다시 포장해야 할 수도 있습니다 ... – Chewy

0

다음은 빠른 통과입니다. 일치하는 값을 기반으로 문자열을 분석하고 스타일을 설정해야하지만 여기서는 시작입니다.

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

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

    try { 
     doc.insertString(doc.getLength(), "This ", null); 
     doc.insertString(doc.getLength(), "Apple", style); 
     doc.insertString(doc.getLength(), " is a good ", null); 
     doc.insertString(doc.getLength(), "Apple", style); 
    } catch (BadLocationException e) { 
    } 

    JFrame frame = new JFrame("Test"); 
    frame.getContentPane().add(textPane); 
    frame.pack(); 
    frame.setVisible(true); 
} 
+0

녹색 색상에 단어 green이 편리하지만 내가 제대로 질문을하지 않았에 apple 매핑. 특정 색상의 특정 단어를 표시하는 사용자 지정 텍스트 편집기를 작성하고 있습니다. 따라서 누군가 텍스트 편집기에서 해당 단어를 입력 할 때마다 지정된 색상의 단어가 표시됩니다. –

+0

아, 네 말이 맞아, 나는 네가 한 말에서 조금 벗어난거야. 이것은 가능하며 사용자 지정 표시기를 사용하여이 작업을 잠시 중단했으나 드로잉 부분에 들어가야한다고 생각합니다. 이것에 대한 오래된 코드를 찾으려고 노력할 것이지만, 그것이 사소한 것은 아니라고 생각합니다. – Chewy

+0

새로운 하나의 도움이 되었습니까 – Chewy