Java - 在JTextArea中将设置颜色设置为文本 [英] Java - Swing setting colour to text in JTextArea
问题描述
我有一个JTextArea,其文本设置为一个信息字符串。在这个信息串中,我有一个变量,我想变成红色,这样我编辑字符串如下:
I have a JTextArea which has its text set to a string of information. In this string of information I have a variable which I would like coloured red, to do this I edit the string as follows:
"Result: <html><font color=red>" + negativeValue + "</font></html>"
我希望这会给出结果:##,其中数字是红色的。但它只是将以下内容放入文本区域:
I would expect this to give Result: ## where the number is red. However it just puts the following into the text area:
Result: <html><font color=red>##</font></html>
我不知道如何让这个工作,所以有人可以提供如何这样做?
I'm not really sure how to get this working, so could someone offer advice as to how to do so?
推荐答案
JTextArea
。如果文字可以是全部一种颜色,请调用 setForeground(Color)
。
JTextArea
is not a component designed for styled text. If the text can be all one color, call setForeground(Color)
.
否则请使用样式文字元件,例如 JEditorPane
或 JTextPane
。更多信息。有关使用它们的信息,请参见如何使用编辑器窗格和文本窗格。
Otherwise use a styled text component such as a JEditorPane
or JTextPane
. For more info. on using them, see How to Use Editor Panes and Text Panes.
还有其他人指出,整个 String
必须以< html>
。
Also as pointed out by others, the entire String
must start with <html>
.
这篇关于Java - 在JTextArea中将设置颜色设置为文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!