![](/img/trans.png)
[英]How can I change the background color of my MainActivity from a second activity using colored buttons as user choices?
[英]How can I change the background color of my textbox for just a second in Java?
我想將文本框的顏色更改為黃色僅一秒鍾,但是我不知道該怎么做。 這是我現在的代碼,它的作用是等待一秒鍾,然后為文本框提供第二種顏色。
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處於休眠狀態,這意味着它已凍結,不會顯示顏色變化或與用戶交互。 因此, 絕對不要在Swing事件線程上調用Thread.sleep(...)
或類似的代碼。
請改用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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.