Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.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_Swing_Jtextpane - Fatal编程技术网

Java JTextPane-关闭最后一个样式

Java JTextPane-关闭最后一个样式,java,swing,jtextpane,Java,Swing,Jtextpane,我想给JTextPane中的一些关键字上色。但是我不想在这些话之后保持同样的风格 while (regexMatcher.find()){ int start = regexMatcher.start(); int end = regexMatcher.end(); document.setCharacterAttributes(start, end-start, style, false); } 但是,

我想给JTextPane中的一些关键字上色。但是我不想在这些话之后保持同样的风格

while (regexMatcher.find()){
            int start = regexMatcher.start();
            int end = regexMatcher.end();

            document.setCharacterAttributes(start, end-start, style, false);
        }
但是,当我在最后一个字符之后单击并键入具有相同样式但希望更改回默认值的内容时,它会起作用


如何解决此问题?

您可以将
CaretChangeListener
添加到
JTextPane
中。在每次插入符号更新时,应清除安装在
JTextPane
中的编辑器的输入属性

MutableAttributeSet inputAttributes=((StyledEditorKit)pane.getEditorKit()).getInputAttributes();
//remove all the unwanted style attributes