2016-06-11 44 views
0

有沒有一種方法可以指定我寫文本時需要的顏色?假設我將「Apple」設置爲紅色。如果我在JTextPane中編寫「這個Apple是很好的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

這是一個使用單詞到顏色映射的解決方案。正如你所看到的,我映射apple爲紅色,字​​對綠色

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圖形用戶界面中編寫單詞時,有沒有辦法讓單詞的顏色發生變化?因爲在我運行該程序並再次寫入「綠色」一詞後,它不會變爲綠色。這實際上是我想要做的。對不起,如果我沒有更好地解釋它。 –

+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

由於這會派上用場,但我沒有問正確的問題。我正在構建一個自定義文本編輯器,以特定顏色顯示某些單詞。因此,無論何時在文本編輯器中鍵入任何這些單詞,它都會以指定的顏色顯示該單詞。 –

+0

啊,你的權利,我跟你說的有點不一樣。這是可能的,我用一個自定義的指標做了一段時間,但我想我必須進入繪圖部分。將嘗試找到一些舊的代碼,但我認爲這不是微不足道的。 – Chewy

+0

新的幫助 – Chewy

相關問題