Swing文本字段中的延迟文本颜色更改 [英] Delayed text color change in Swing text field
本文介绍了Swing文本字段中的延迟文本颜色更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以更改文本字段中文本的颜色?我正在尝试构建一个解释器,所以我想知道如何实时更改文本的颜色。
例如我在文本字段中输入的单词是:
Is it possible to change the color of a text in a text field?I am trying to build an interpreter, so I was wondering on how would you change the color of the text in real time. For example the word I enter in the text field is:
printf("hi");
单词 printf
在几个之后变为绿色秒。
The word printf
becomes green after a few seconds.
有可能吗?
推荐答案
package test;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class BlinkColorTextField {
BlinkColorTextField() {
final JTextField blinkingText = new JTextField("Red & Blue");
ActionListener blinker = new ActionListener() {
boolean isRed = true;
public void actionPerformed(ActionEvent ae) {
if (isRed) {
blinkingText.setForeground(Color.BLUE);
} else {
blinkingText.setForeground(Color.RED);
}
isRed = !isRed;
}
};
Timer timer = new Timer(1000, blinker);
timer.start();
JOptionPane.showMessageDialog(null, blinkingText);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new BlinkColorTextField();
}
});
}
}
这篇关于Swing文本字段中的延迟文本颜色更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文