如何更改JTextPane中特定单词的颜色? [英] How to change the color of specific words in a JTextPane?
本文介绍了如何更改JTextPane中特定单词的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在用户输入时更改 JTextPane
中特定单词的颜色?
我应该覆盖 JTextPane
paintComponent
方法?
How do I change the color of specific words in a JTextPane
just while the user is typing?
Should I override JTextPane
paintComponent
method?
推荐答案
覆盖 paintComponent
对你没有帮助。
这不是一个简单的,但也不是不可能。这样的事情会对你有所帮助:
This is not an easy one, but not impossible either. Something like this will help you:
DefaultStyledDocument document = new DefaultStyledDocument();
JTextPane textpane = new JTextPane(document);
StyleContext context = new StyleContext();
// build a style
Style style = context.addStyle("test", null);
// set some style properties
StyleConstants.setForeground(style, Color.BLUE);
// add some data to the document
document.insertString(0, "", style);
你可能需要调整一下,但至少会告诉你从哪里开始。
You may need to tweak this, but at least it shows you where to start.
这篇关于如何更改JTextPane中特定单词的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文