我想将文本框的颜色更改为黄色仅一秒钟,但是我不知道该怎么做。这是我现在的代码,它的作用是等待一秒钟,然后为文本框提供第二种颜色。
for(int i=0;i<2;i++){
if(i==0)
{
textbox1.setBackground(Color.yellow); //Turn textbox yellow (first color)
try {
TimeUnit.SECONDS.sleep(1); //wait 1 second
}
catch (InterruptedException e) {}
}
else if(i==1)
{
textbox1.setBackground(Color.white); //Turn textbox white (second color)
}
}
附言 我也尝试过Thread.sleep(1000); 安装了TimeUnit.SECONDS.sleep(1);
使用当前代码,您将整个GUI置于睡眠状态,这意味着它已冻结,不会显示颜色变化或与用户交互。因此,绝对不要Thread.sleep(...)
在Swing事件线程上调用或类似的代码。
请改用Swing计时器,因为它是专门为这种目的而构建的,以提供一次或重复的时间延迟的Swing代码。
例如,
textbox1.setBackground(Color.yellow);
int delayTime = 3 * 1000; // 3 seconds
new Timer(delayTime, new ActionListener() {
public void actionPerformed(ActionEvent e) {
textbox1.setBackground(Color.white);
// stop the timer
((Timer) e.getSource()).stop();
}
}).start();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句