Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 每当我在JTextPane中写入特定单词时,请指定文本颜色_Java_Jtextarea_Text Editor_Jtextpane - Fatal编程技术网

Java 每当我在JTextPane中写入特定单词时,请指定文本颜色

Java 每当我在JTextPane中写入特定单词时,请指定文本颜色,java,jtextarea,text-editor,jtextpane,Java,Jtextarea,Text Editor,Jtextpane,有没有一种方法可以指定我希望文字在书写时的颜色?假设我将“苹果”设置为红色。如果我在JTextPane中写“这个苹果是好苹果”,那么“苹果”一词应该变成红色 这是我到目前为止看到的,但它只显示了全部黑色。我想让单词Apple显示为红色 public static void main(String[] args) { JTextPane textPane = new JTextPane(); //Would like to make the words Apple Red in f

有没有一种方法可以指定我希望文字在书写时的颜色?假设我将“苹果”设置为红色。如果我在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);
}

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

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

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

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;
    }
公共类测试{
HashMap myMap=新HashMap();
公共静态void main(字符串[]args){
新测试();
}
公开考试(){
myMap.put(“苹果”,颜色为红色);
我的地图。放(“苹果”,颜色。红色);
myMap.put(“绿色”,Color.green);
String text=“这是一个绿苹果,我喜欢吃苹果”;
JTextPane textPane=新的JTextPane();
StyledDocument doc=textPane.getStyledDocument();
Style Style=textPane.addStyle(“红色样式”,null);
设置前景(样式、颜色、红色);
ArrayList chunks=getColorsBasedOnText(文本,文本窗格);
试一试{
for(块:块){
doc.insertString(doc.getLength(),chunk.text+“”,chunk.style);
}
}捕获(错误位置异常e){
}
JFrame=新JFrame(“测试”);
frame.getContentPane().add(textPane);
frame.pack();
frame.setVisible(true);
}
私有ArrayList getColorsBasedOnText(字符串文本,JTextPane textPane){
ArrayList chunks=新的ArrayList();
for(字符串字:text.split(“”){
Chunk Chunk=新Chunk();
chunk.text=单词;
Color=myMap.get(word.toLowerCase());
如果(颜色!=null){
chunk.style=textPane.addStyle(“style”,null);
setForeground(chunk.style,color);
}
chunk.add(chunk);
}
返回块;
}
私有类块{
公共字符串文本;
公共风格;
}

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

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;
    }
公共类测试{
HashMap myMap=新HashMap();
公共静态void main(字符串[]args){
新测试();
}
公开考试(){
myMap.put(“苹果”,颜色为红色);
我的地图。放(“苹果”,颜色。红色);
myMap.put(“绿色”,Color.green);
String text=“这是一个绿苹果,我喜欢吃苹果”;
JTextPane textPane=新的JTextPane();
StyledDocument doc=textPane.getStyledDocument();
Style Style=textPane.addStyle(“红色样式”,null);
设置前景(样式、颜色、红色);
ArrayList chunks=getColorsBasedOnText(文本,文本窗格);
试一试{
for(块:块){
doc.insertString(doc.getLength(),chunk.text+“”,chunk.style);
}
}捕获(错误位置异常e){
}
JFrame=新JFrame(“测试”);
frame.getContentPane().add(textPane);
frame.pack();
frame.setVisible(true);
}
私有ArrayList getColorsBasedOnText(字符串文本,JTextPane textPane){
ArrayList chunks=新的ArrayList();
for(字符串字:text.split(“”){
Chunk Chunk=新Chunk();
chunk.text=单词;
Color=myMap.get(word.toLowerCase());
如果(颜色!=null){
chunk.style=textPane.addStyle(“style”,null);
setForeground(chunk.style,color);
}
chunk.add(chunk);
}
返回块;
}
私有类块{
公共字符串文本;
公共风格;
}

谢谢这会派上用场,但我没有正确地问这个问题。我正在构建一个自定义文本编辑器,以特定颜色显示某些单词。因此,每当有人在文本编辑器中键入这些单词中的任何一个,它都会以指定的颜色显示该单词。啊,你的权利,我与你说的有点不一致。这是可能的,我做到了这是一个定制的必要指标,但我想我必须进入绘图部分。我会试图找到一些关于这一点的旧代码,但我认为这不是微不足道的。新的帮助这几乎正是我所需要的。它显示了正确的结果,但我真正需要的是正如你所说的,根据匹配的值设置样式。这就是r编辑在编辑器中编写单词后更改单词的颜色,而不是程序中预定义的单词。启动了一个版本,但我要出去吃饭…应该不会太糟糕,只是取决于您正在搜索多少不同的颜色和单词。您有信息吗谢谢这会派上用场,但我有问得不对。我正在构建一个自定义文本编辑器,以特定的颜色显示某些单词。因此,每当有人在文本编辑器中键入这些单词时,它都会以指定的颜色显示该单词。啊,你说的对,我与你说的有点不一致。这是可能的,我不久前用自定义的所需标记做了这件事tor,但我认为我必须进入绘图部分。我将尝试在这方面找到一些旧代码,但我认为这不是小事。新代码是否有帮助这几乎正是我所需要的。它显示正确的结果,但我真正需要的是,正如你所说的,根据匹配的值设置样式。也就是说,编辑器可以更改单词af的颜色ter它是在编辑器中编写的,不是预先定义的单词