2017-03-24 40 views
1

我想创建一个简单的TextEditor程序,它可以在字符串中找到所有'a'字符并将颜色改为红色。我可以找到'a'字符,所以我只需要改变颜色。如果它不可能在java中,我可以做这在c + +(QT Lib。)?如何更改所有'a'字符是字符串并在JEditorPane或JTextArea中显示?

+3

[JEditorPane为不同的单词设置前景色的可能的副本](http://stackoverflow.com/questions/18948148/jeditorpane-set-foreground-color-for-different-words) – Berger

+0

谢谢,这是好方法。 –

回答

1

Java中的JEditor窗格支持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; }"); 

我希望我帮你。

相关问题