如何将所有 'a' 个字符更改为字符串并显示在 JEditorPane 或 JTextArea 中?

How to change all of 'a' characters is string and show in JEditorPane or JTextArea?

我想创建简单的 TextEditor 程序,找到字符串中所有 'a' 个字符并将颜色更改为 red.I 可以找到 'a' 个字符,所以我只需要更改 color.If 在 java 中是不可能的,我可以在 c++(QT Lib.) 中这样做吗?

Java 中的 JEditor Pane 支持 HTML 和 CSS。因此,将 html 和 css 代码用于您想要的任何内容,例如更改颜色、粗体和斜体等。

pane  = new JEditorPane();
pane.setContentType("text/html");

你可以直接写html和内联css。

对于高级水平,您还可以使用 HTMLEditorKit class 添加 css.

HTMLEditorKit kit = new HTMLEditorKit();
jEditorPane.setEditorKit(kit);
StyleSheet styleSheet = kit.getStyleSheet();
styleSheet.addRule("body {color:#000; font-family:times; margin: 4px; }");
styleSheet.addRule("h1 {color: blue;}");
styleSheet.addRule("h2 {color: #ff0000;}");
styleSheet.addRule("pre {font : 10px monaco; color : black; background-color : #fafafa; }");

希望对你有所帮助。