控制器:
package sample;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.ResourceBundle;
import java.util.TimeZone;
public class Controller implements Initializable {
@FXML
private Label daysLeft;
@Override
public void initialize(URL location, ResourceBundle resources) {
Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
Date date;
int dayOfYear;
int hoursRemainingInDay;
String daysLeftString;
date = calendar.getTime();
dayOfYear = calendar.get(Calendar.DAY_OF_YEAR);
daysLeftString = "" + (139 - dayOfYear);
hoursRemainingInDay = (24-getCurrentHourOfDay());
daysLeft.setStyle("-fx-font: 48 arial");
// daysLeft.setText(daysLeftString);
daysLeft.setText(daysLeftString+"."+ hoursRemainingInDay);
try {
Thread.sleep(1_000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static int getCurrentHourOfDay() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH");
Date date = new Date(System.currentTimeMillis());
return Integer.valueOf(simpleDateFormat.format(date)); // Return hours of day
}
public void update(){
}
}
我希望它每小时更新一次。我试过了 而(真) 睡了一个小时 但是while(true)做到了,所以窗口从未打开过。