use*_*609 1 java string swing text colors
我想知道如何更改句子中的特定文本颜色?
让我们说HELLO WORLD ...我想把WORLD改成红色而不改变HELLO的字体颜色.同样如何将WORLD改为粗体
我想将这些字符串设置为jtextarea,但我能找到的就是这样的
JTextArea textbox = new JTextArea("hello world");
textbox.setForeground(Color.red)
Run Code Online (Sandbox Code Playgroud)
这些使得整个句子变成红色而不是仅仅将WORLD变成红色?
从Oracle文档组件文档中了解这一点.A JTextArea将接受样式,但它将始终在其整个内容中应用样式.但是,如果您要使用a JTextPane,则可以使用HTML在文本中创建所需的任何样式.
备份断言的代码:
import javax.swing.JFrame;
import javax.swing.JTextPane;
import javax.swing.text.html.HTMLEditorKit;
public class StyleTestApp {
public static void main(final String[] args) {
final JFrame f = new JFrame("test");
//f.getContentPane().add(new JTextArea("<html>HELLO <font size=\"3\" face=\"verdana\" color=\"red\">WORLD</font></html>"));
final JTextPane p = new JTextPane();
// the HTMLEditorKit is not enabled by default in the JTextPane class.
p.setEditorKit(new HTMLEditorKit());
p.setText("<html>HELLO <font size=\"3\" face=\"verdana\" color=\"red\">WORLD</font></html>");
f.getContentPane().add(p);
f.pack();
f.setVisible(true);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
25337 次 |
| 最近记录: |