gpt4 book ai didi

java - Swing 文本字段中的文本颜色更改延迟

转载 作者:行者123 更新时间:2023-12-02 05:16:47 24 4
gpt4 key购买 nike

是否可以更改文本字段中文本的颜色?我正在尝试构建一个解释器,所以我想知道如何实时更改文本的颜色。例如,我在文本字段中输入的单词是:

printf("hi");

几秒钟后,printf 一词变为绿色。

可能吗?

最佳答案

BlinkColorTextField

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();
}
});
}

}

关于java - Swing 文本字段中的文本颜色更改延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9803658/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com