这篇文章与我的上一篇文章有关。代码块在所需时间后将文本更改为所需的颜色。但是,现在我想更改特定单词的颜色,以便每个字母获得相同的时间。例如,如果“你好”的时间为 1000 毫秒(有 5 个字母),那么 'h''e''l' 'l''o' 每个字母应该得到 1000/5 毫秒,即每个 200 毫秒。
我为此实现了摆动计时器:
public Reminder() {
a[0]=2000;
a[1]=1000;
a[2]=3000;
a[3]=5000;
a[4]=3000;
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
point =point +arr[i].length();
i++;
doc.setCharacterAttributes(0,point+1, textpane.getStyle("Red"), true);
timer.setDelay(a[i]);
}
};
timer = new Timer(a[i], actionListener);
timer.setInitialDelay(0);
timer.start();
为了发生这种情况,我应该Timer
在内部使用另一个actionListener
来为特定字母提供更多时间吗?还是我应该先打破时间.length()
然后使用计时器?我无法决定更好的方法。有什么想法吗?