我想创建一个简单的TextEditor程序,该程序在String中找到所有'a'字符并将颜色更改为红色。我可以找到'a'字符,因此我只需要更改颜色。如果在Java中是不可能的,我可以在c++(QT库)?

最佳答案

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

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

您可以直接编写html和内联CSS。

对于高级级别,您还可以使用HTMLEditorKit类添加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; }");

希望对您有帮助。

关于java - 如何更改所有 'a'字符为字符串并显示在JEditorPane或JTextArea中?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42997583/

10-13 00:04