I am working on a clock program in Java and I am unsure how to make the time update secondly. I have tried using a for loop with thread.sleep(1000);
but that did not work. Also, if anybody knows how to stop the Stage
from opening in white then having a small delay before turning black, that would be really appreciated.
Here is my code:
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.scene.paint.Color;
import javafx.scene.layout.HBox;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import java.text.DateFormat;
import java.util.Date;
import java.text.SimpleDateFormat;
public class Clock extends Application {
@Override public void start(Stage stage) {
DateFormat df = new SimpleDateFormat("EEE,MMM d yyyy - h:mm:ss a");
Date date = new Date();
String stringDate = df.format(date);
Text text = new Text(10, 60, stringDate);
text.setFont(Font.font ("Digital Dream Fat", 30f));
text.setFill(Color.RED);
HBox hbox = new HBox();
Scene scene = new Scene(new Group(text));
scene.setFill(Color.BLACK);
stage.setScene(scene);
stage.initStyle(StageStyle.UNDECORATED);
stage.setWidth(710);
stage.setHeight(80);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}