[英]Java Timer not working
我有一個名為worldImageToUse
的Image
並且有一個Timer
,它應該每1秒在兩個圖像之間切換worldImageToUse
。 但這似乎不起作用。 請幫助?
public void startWorldImageFlash() {
worldImageFlashTimer = new Timer();
TimerTask task = new TimerTask() {
@Override
public void run() {
if(worldImageToUse == worldImage) setWorldImageBW();
if(worldImageToUse == worldImageBW) setWorldImageColor();
}
};
worldImageFlashTimer.scheduleAtFixedRate(task, 0, 1000);
}
public void stopWorldImageFlash() {
worldImageFlashTimer.cancel();
setWorldImageColor();
}
檢查兩次,將第二個if更改為“ else if”,即可解決問題。 另外,在這種情況下,您應該考慮調試:)
看起來您的代碼說的是如果顏色設置為黑白。 然后說如果黑白設置為彩色。 您是否每次都會得到相同的圖像。 您的第二個if如果需要則為else if。
是否在設置圖像后repaint()
組件?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.