每当我在 JTextPane 中写一个特定的单词时指定文本颜色

Specify text color whenever I write a particular word in JTextPane

有没有办法指定我希望文本在书写时具有的颜色?假设我将 "Apple" 设置为红色。如果我在 JTextPane 中写 "this Apple is good Apple",单词 "apple" 应该变成红色。

这是我目前所拥有的,但它只显示全黑。我想让单词 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);
}

这是一个快速传递。需要解析字符串并根据匹配值设置样式,但这里是一个开始。

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);
}

这是一个使用文字到颜色映射的解决方案。如您所见,我将 apple 映射为红色,将单词 green 映射为绿色

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;
    }