[英]Java GUI not launching after Thread.sleep
我正在嘗試在滿足特定條件但滿足條件時彈出一個窗口。 窗口沒有打開。我正在使用Thread.sleep
碼:
public void grow() {
Thread thread = new Thread(() -> {
try {
Thread.sleep(this.harvestTime);
if(water >= waterNeeded && fertelizer >= fertelizerNeeded) {
this.harvest = true;
AlertBox.display("CROP ALERT","A SEED HAS FINISHED GROWING");
Thread.sleep(60000);
if(harvest == true) {
withered = true;
harvest = false;
AlertBox.display("ALERT", "FAILED TO HARVEST A CROP. IT BECAME WITHERED!");
}
}
else {
this.withered = true;
}
} catch (Exception e) {
}
});
thread.start();
}
Java FX方法僅在Java FX線程中有效。 您不能也不應使用后台線程中的Java FX組件/方法。
如果您需要顯示來自后台線程的通知,請使用:
Platform.runLater(new Runnable() {
@Override public void run() {
bar.setProgress(counter / 1000000.0);
}
});
在這種情況下, Runnable
將被添加到Java FX事件隊列中,並將在Java FX線程中進行處理。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.