Advertisement
rjsantiago0001

Set Clock Time

Apr 16th, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.68 KB | None | 0 0
  1. // Ricardo Santiago
  2. // 4/16/16
  3. // CSCI-112
  4. // HW#15 Clock with user input
  5.  
  6. import javafx.application.Application;
  7. import javafx.stage.Stage;
  8. import javafx.animation.KeyFrame;
  9. import javafx.animation.Timeline;
  10. import javafx.event.ActionEvent;
  11. import javafx.event.EventHandler;
  12. import javafx.geometry.Insets;
  13. import javafx.geometry.Pos;
  14. import javafx.scene.Scene;
  15. import javafx.scene.control.Button;
  16. import javafx.scene.control.ContentDisplay;
  17. import javafx.scene.control.Label;
  18. import javafx.scene.control.TextField;
  19. import javafx.scene.input.MouseEvent;
  20. import javafx.scene.layout.BorderPane;
  21. import javafx.scene.layout.HBox;
  22. import javafx.scene.layout.VBox;
  23. import javafx.util.Duration;
  24.  
  25. public class SetClockTime extends Application {
  26.  
  27.     Timeline animation;
  28.     TextField tfHour,tfMinutes,tfSeconds;
  29.   public void start(Stage primaryStage) {
  30.      
  31.     ClockPane clock = new ClockPane(); // Create a clock
  32.     Button btStart = new Button("Start");
  33.     Button btStop = new Button("Stop");
  34.    
  35.     // Hour
  36.     tfHour = new TextField(clock.getHour() + "");
  37.     tfHour.setPrefColumnCount(2);
  38.     Label lblHour = new Label("Hour:", tfHour);
  39.     lblHour.setContentDisplay(ContentDisplay.RIGHT);
  40.     tfHour.setOnAction(e-> clock.setHour(Integer.parseInt(tfHour.getText())));
  41.  
  42.     // Minutes
  43.     tfMinutes = new TextField(clock.getMinute() + "");
  44.     tfMinutes.setPrefColumnCount(2);
  45.     Label lblMinutes = new Label("Minute:", tfMinutes);
  46.     lblMinutes.setContentDisplay(ContentDisplay.RIGHT);
  47.     tfMinutes.setOnAction(e-> clock.setMinute(Integer.parseInt(tfMinutes.getText())));
  48.  
  49.     // Seconds
  50.     tfSeconds = new TextField(clock.getSecond() + "");
  51.     tfSeconds.setPrefColumnCount(2);
  52.     Label lblSeconds = new Label("Second:", tfSeconds);
  53.     lblSeconds.setContentDisplay(ContentDisplay.RIGHT);
  54.     tfSeconds.setOnAction(e-> clock.setSecond(Integer.parseInt(tfSeconds.getText())));
  55.  
  56.  
  57.     // Create a handler for animation
  58.     EventHandler<ActionEvent> eventHandler = e -> {
  59.       clock.setCurrentTime(); // Set a new clock time
  60.      
  61.     };
  62.    
  63.     // Button to start clock
  64.     btStart.setOnMousePressed((MouseEvent e) -> {
  65.      animation = new Timeline(
  66.               new KeyFrame(Duration.millis(1000), eventHandler));
  67.             animation.setCycleCount(Timeline.INDEFINITE);
  68.             animation.play(); // Start animation
  69.     });
  70.    
  71.     // Button to stop clock
  72.     btStop.setOnMousePressed((MouseEvent e) -> {
  73.         animation.stop();
  74.     });
  75.    
  76.     BorderPane pane = new BorderPane(clock);
  77.     BorderPane.setAlignment(clock, Pos.CENTER);
  78.  
  79.     HBox textFieldPane = new HBox(5);
  80.     textFieldPane.setAlignment(Pos.BOTTOM_CENTER);
  81.     textFieldPane.getChildren().addAll(lblHour, lblMinutes, lblSeconds, btStart, btStop);
  82.     textFieldPane.setPadding(new Insets(5));
  83.    
  84.     VBox buttonPane = new VBox(5);
  85.     buttonPane.setAlignment(Pos.CENTER_LEFT);
  86.     buttonPane.getChildren().addAll(btStart, btStop);
  87.     buttonPane.setPadding(new Insets(5));
  88.    
  89.     pane.setBottom(textFieldPane);
  90.     pane.setLeft(buttonPane);
  91.     primaryStage.setScene(new Scene(pane,400,400));
  92.     primaryStage.setTitle("Clock");
  93.     primaryStage.show();
  94.     clock.setFocusTraversable(true);
  95.    
  96.  
  97.     // Create a scene and place it in the stage
  98. //    Scene scene = new Scene(clock, 250, 250);
  99. //    primaryStage.setTitle("ClockAnimation"); // Set the stage title
  100. //    primaryStage.setScene(scene); // Place the scene in the stage
  101. //    primaryStage.show(); // Display the stage
  102.   }
  103.  
  104.   /**
  105.    * The main method is only needed for the IDE with limited
  106.    * JavaFX support. Not needed for running from the command line.
  107.    */
  108.   public static void main(String[] args) {
  109.     launch(args);
  110.   }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement