Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.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 如何从某一点向前设置文本的颜色_Java_Swing_Netbeans_Colors_Jtextpane - Fatal编程技术网

Java 如何从某一点向前设置文本的颜色

Java 如何从某一点向前设置文本的颜色,java,swing,netbeans,colors,jtextpane,Java,Swing,Netbeans,Colors,Jtextpane,我正在构建一个小型对话代理,其中的文本如下所示: 我想将系统的文本设置为始终为红色。文本全部放置在JTextPane中 我怎样才能做到这一点?我尝试了以下几点: agentTextPane.setForegroundColor.red;添加系统文本后,然后切换回黑色,但这会更改JTextPane中的所有文本 以下是添加系统文本的方式: //'output' is a stringBuilder output.append("\nSystem: ").append(tempOutput).appe

我正在构建一个小型对话代理,其中的文本如下所示:

我想将系统的文本设置为始终为红色。文本全部放置在JTextPane中

我怎样才能做到这一点?我尝试了以下几点:

agentTextPane.setForegroundColor.red;添加系统文本后,然后切换回黑色,但这会更改JTextPane中的所有文本

以下是添加系统文本的方式:

//'output' is a stringBuilder
output.append("\nSystem: ").append(tempOutput).append("\n");
agentTextPane.setText(output.toString());
如图所示,可以定义表示所需样式的属性集。比如说,

StyledDocument doc = (StyledDocument) jtp.getDocument();
SimpleAttributeSet system = new SimpleAttributeSet();
StyleConstants.setFontFamily(system, "Serif");
StyleConstants.setForeground(system, Color.red);
doc.insertString(doc.getLength(), "...", system);
样式可以是渐进式的,如图所示

有关更多示例,请参见。

如图所示,您可以定义表示所需样式的属性集。比如说,

StyledDocument doc = (StyledDocument) jtp.getDocument();
SimpleAttributeSet system = new SimpleAttributeSet();
StyleConstants.setFontFamily(system, "Serif");
StyleConstants.setForeground(system, Color.red);
doc.insertString(doc.getLength(), "...", system);
样式可以是渐进式的,如图所示


有关更多示例,请参阅。

您可能希望使用HTML标记来设置字符串的颜色格式。以下参考资料可能有用


您可能希望使用HTML标记来设置字符串的颜色格式。以下参考资料可能有用


你能给我们看一些你的代码吗?@blackpanther-我添加了一些代码,但我不能再添加更多了。@Trashgood,取消删除。你能给我们看一些你的代码吗?@blackpanther-我添加了一些代码,但我不能再添加更多了。@Trashgood,取消删除。