2016-06-11 67 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

相关问题