我正在为代码编辑器编写脚本,我想要动态命令。如何动态更改JTextArea中的字体颜色?
所以,如果用户键入“class”,它将改变“class”的颜色。
我该怎么做?
// This is the main focus part of the code.
textarea.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent evt) {
word += evt.getKeyChar();
if(evt.getKeyCode() == KeyEvent.VK_ENTER) {
word = "";
line = "";
lineInMemory = line;
}
if(evt.getKeyCode() == KeyEvent.VK_SPACE) {
word = word.replaceAll("null","");
line += word;
word = "";
String text = textarea.getText();
String[] words = line.split(" ");
if(word.toLowerCase().equals("class")) {
// What the heck do I put here?!
}
}
}
});
我已经拥有了读键的关键听众,将它们放入单词中,然后将单词放入句子中。我希望它能够输入关键字,并且它在键入时会自动更改关键字的颜色,有点像Sublime Text的功能。
欢迎来到StackOverflow,我们不是您可能会看到的代码工厂。您应该发布您的代码或至少一个[最小示例](http://stackoverflow.com/help/mcve),以便我们可以复制粘贴并帮助您。如果你没有表现出任何努力来解决它,那么我认为任何人都不会试图用这个来帮助你。 – Frakcool 2014-08-29 17:16:36
是的,我正在编辑文章,因为你发布它。谢谢! – mgthomas99 2014-08-29 17:21:35