我正在尝试实现一个计时器,并且希望它显示在屏幕上。我能够使它显示出来,但不能在每个屏幕上递增。有人可以帮我吗?
代码如下:
// Screen Size
int gameWidth = 1000;
int gameHeight = 700;
// Timer
long endTime = System.currentTimeMillis();
Label timeLabel = new Label();
DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
final Timeline timeline = new Timeline(
new KeyFrame(
Duration.seconds(1),
event -> {
final long diff = endTime - System.currentTimeMillis();
if (diff < 0) {
// timeLabel.setText("00:00:00");
timeLabel.setText(timeFormat.format(0));
} else {
timeLabel.setText(timeFormat.format(diff));
}
})
);
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.play();
// Background Image
StackPane gameBackgroundImgContainer = new StackPane();
ImageView gameBgImage = new ImageView("stoneBG.jpg");
gameBgImage.setFitHeight(gameHeight + 100);
gameBgImage.setFitWidth(gameWidth + 500);
gameBackgroundImgContainer.getChildren().addAll(gameBgImage, gameBorder);
game = new Scene(gameBackgroundImgContainer, gameWidth, gameHeight);