Advertisement
PadmaJS

unfinished Javafx lab 9

Nov 26th, 2022
2,075
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 3.70 KB | None | 0 0
  1. import javafx.animation.KeyFrame;
  2. import javafx.animation.Timeline;
  3. import javafx.application.Application;
  4. import javafx.geometry.Insets;
  5. import javafx.scene.Scene;
  6. import javafx.scene.control.Button;
  7. import javafx.scene.control.Label;
  8. import javafx.scene.control.TextField;
  9. import javafx.scene.layout.AnchorPane;
  10. import javafx.scene.layout.GridPane;
  11. import javafx.stage.Stage;
  12. import javafx.util.Duration;
  13.  
  14. public class Microwave extends Application {
  15.     static Timeline timeline;
  16.  
  17.     static int time;
  18.  
  19.     public static void main(String[] args) throws Exception {
  20.         launch(args);
  21.     }
  22.  
  23.     @Override
  24.     public void start(Stage stage) throws Exception {
  25.         // creating label
  26.         Label label = new Label("Place Food Here");
  27.         AnchorPane anchor_pane = new AnchorPane(label);
  28.         AnchorPane.setTopAnchor(label, 70.0);
  29.  
  30.         // creating textfield
  31.         TextField text_field = new TextField("Time to be displayed here");
  32.         text_field.setMinWidth(250.0);
  33.         AnchorPane.setLeftAnchor(text_field, 85.0);
  34.         anchor_pane.getChildren().add(text_field);
  35.  
  36.         // creating buttons on gridpane
  37.         GridPane grid_pane = new GridPane();
  38.  
  39.         // Add buttons to the panel
  40.         int count = 1;
  41.         for (int row = 0; row < 3; row++) {
  42.             for (int col = 0; col < 3; col++) {
  43.                 Button btn = buttonContent(String.valueOf(count));
  44.                 btn.setOnMouseClicked(e -> {
  45.                     checkTextField(text_field);
  46.                     text_field.setText(text_field.getText() + btn.getText());
  47.                 });
  48.                 grid_pane.add(btn, col, row);
  49.                 count++;
  50.             }
  51.         }
  52.         // 0 button
  53.         Button btn0 = buttonContent(String.valueOf(0));
  54.         btn0.setOnMouseClicked(e -> {
  55.             checkTextField(text_field);
  56.             text_field.setText(text_field.getText() + btn0.getText());
  57.         });
  58.         grid_pane.add(btn0, 0, 3);
  59.  
  60.         // start button
  61.         Button start_btn = buttonContent("Start");
  62.         start_btn.setOnMouseClicked(e -> {
  63.             startTimer(text_field);
  64.         });
  65.         grid_pane.add(start_btn, 1, 3);
  66.  
  67.         // stop button
  68.         Button stop_btn = buttonContent("Stop");
  69.         stop_btn.setOnMouseClicked(e -> {
  70.             stopTimer();
  71.         });
  72.         grid_pane.add(stop_btn, 2, 3);
  73.  
  74.         grid_pane.setStyle("-fx-border-color:rgba(255,0,0,1);");
  75.         grid_pane.setHgap(4);
  76.         grid_pane.setVgap(4);
  77.         grid_pane.setPadding(new Insets(10, 9, 10, 13));
  78.         AnchorPane.setTopAnchor(grid_pane, 25.0);
  79.         AnchorPane.setLeftAnchor(grid_pane, 85.0);
  80.         anchor_pane.getChildren().add(grid_pane);
  81.  
  82.         Scene scene = new Scene(anchor_pane, 318, 159);
  83.         stage.setTitle("Microwave Oven");
  84.         stage.setScene(scene);
  85.         stage.show();
  86.     }
  87.  
  88.     private static Button buttonContent(String text) {
  89.         Button b = new Button(text);
  90.         b.setMinWidth(67);
  91.  
  92.         return b;
  93.     }
  94.  
  95.     private static void checkTextField(TextField tx) {
  96.         if (tx.getText().equals("Time to be displayed here")) {
  97.             tx.setText("");
  98.         }
  99.     }
  100.  
  101.     private static void startTimer(TextField tx) {
  102.         time = Integer.parseInt(tx.getText());
  103.         KeyFrame kf = new KeyFrame(Duration.millis(1000), e -> {
  104.             time--;
  105.             if (time == 0) {
  106.                 timeline.stop();
  107.             }
  108.             tx.setText(Integer.toString(time));
  109.         });
  110.  
  111.         timeline = new Timeline(kf);
  112.         timeline.setCycleCount(Timeline.INDEFINITE);
  113.         timeline.play();
  114.     }
  115.  
  116.     private static void stopTimer() {
  117.         timeline.pause();
  118.     }
  119. }
  120.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement