Make Digital Clock in JavaFX
Make Digital Clock in JavaFX - YouTube
________________________________________________ How to Make Digital Clock in JavaFX?
import java.util.Calendar;
import java.util.GregorianCalendar;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.util.Duration;
public class JavaFXDigitalClock extends Application{
Integer hour = 0;
Integer minute = 0;
Integer sec = 0;
public void start(Stage stage) {
GridPane pane = new GridPane();
pane.setAlignment(Pos.CENTER);
pane.setStyle("-fx-background-color: BLACK;");
Label lblSec = new Label();
lblSec.setText(sec.toString());
lblSec.setStyle("-fx-text-fill:CYAN; -fx-font-size:40;");
pane.add(lblSec, 4, 0);
Label lblDot = new Label(":");
lblDot.setStyle("-fx-text-fill:CYAN; -fx-font-size:40;");
pane.add(lblDot, 3, 0);
Label lblMin = new Label();
lblMin.setText(minute.toString());
lblMin.setStyle("-fx-text-fill:CYAN; -fx-font-size:40;");
pane.add(lblMin, 2, 0);
Label lblDot1 = new Label(":");
lblDot1.setStyle("-fx-text-fill:CYAN; -fx-font-size:40;");
pane.add(lblDot1, 1, 0);
Label lblHour = new Label();
lblHour.setText(hour.toString());
lblHour.setStyle("-fx-text-fill:CYAN; -fx-font-size:40;");
pane.add(lblHour, 0, 0);
EventHandler<ActionEvent> eventHandler = e ->{
Calendar calendar = new GregorianCalendar();
this.hour = calendar.get(Calendar.HOUR_OF_DAY);
this.minute = calendar.get(Calendar.MINUTE);
this.sec = calendar.get(Calendar.SECOND);
lblSec.setText(sec.toString());
lblMin.setText(minute.toString());
lblHour.setText(hour.toString());
};
Timeline tLine = new Timeline(new KeyFrame(Duration.millis(1000), eventHandler));
tLine.setCycleCount(Timeline.INDEFINITE);
tLine.play();
Scene scene = new Scene(pane, 150, 50);
stage.setTitle("JavaFX Digital Clock");
stage.setResizable(false);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
_____________________________________________
Dark Hers
YouTube
https://instagram.com/darkhers
https://www.facebook.com/darkhers
TikTok
https://www.tiktok.com/@darkhers
https://www.pinterest.co.uk/darkhers
Comments
Post a Comment