Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javafx.animation.KeyFrame;
- import javafx.animation.Timeline;
- import javafx.application.Application;
- import javafx.geometry.Insets;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.control.Label;
- import javafx.scene.control.TextField;
- import javafx.scene.layout.AnchorPane;
- import javafx.scene.layout.GridPane;
- import javafx.stage.Stage;
- import javafx.util.Duration;
- public class Microwave extends Application {
- static Timeline timeline;
- static int time;
- public static void main(String[] args) throws Exception {
- launch(args);
- }
- @Override
- public void start(Stage stage) throws Exception {
- // creating label
- Label label = new Label("Place Food Here");
- AnchorPane anchor_pane = new AnchorPane(label);
- AnchorPane.setTopAnchor(label, 70.0);
- // creating textfield
- TextField text_field = new TextField("Time to be displayed here");
- text_field.setMinWidth(250.0);
- AnchorPane.setLeftAnchor(text_field, 85.0);
- anchor_pane.getChildren().add(text_field);
- // creating buttons on gridpane
- GridPane grid_pane = new GridPane();
- // Add buttons to the panel
- int count = 1;
- for (int row = 0; row < 3; row++) {
- for (int col = 0; col < 3; col++) {
- Button btn = buttonContent(String.valueOf(count));
- btn.setOnMouseClicked(e -> {
- checkTextField(text_field);
- text_field.setText(text_field.getText() + btn.getText());
- });
- grid_pane.add(btn, col, row);
- count++;
- }
- }
- // 0 button
- Button btn0 = buttonContent(String.valueOf(0));
- btn0.setOnMouseClicked(e -> {
- checkTextField(text_field);
- text_field.setText(text_field.getText() + btn0.getText());
- });
- grid_pane.add(btn0, 0, 3);
- // start button
- Button start_btn = buttonContent("Start");
- start_btn.setOnMouseClicked(e -> {
- startTimer(text_field);
- });
- grid_pane.add(start_btn, 1, 3);
- // stop button
- Button stop_btn = buttonContent("Stop");
- stop_btn.setOnMouseClicked(e -> {
- stopTimer();
- });
- grid_pane.add(stop_btn, 2, 3);
- grid_pane.setStyle("-fx-border-color:rgba(255,0,0,1);");
- grid_pane.setHgap(4);
- grid_pane.setVgap(4);
- grid_pane.setPadding(new Insets(10, 9, 10, 13));
- AnchorPane.setTopAnchor(grid_pane, 25.0);
- AnchorPane.setLeftAnchor(grid_pane, 85.0);
- anchor_pane.getChildren().add(grid_pane);
- Scene scene = new Scene(anchor_pane, 318, 159);
- stage.setTitle("Microwave Oven");
- stage.setScene(scene);
- stage.show();
- }
- private static Button buttonContent(String text) {
- Button b = new Button(text);
- b.setMinWidth(67);
- return b;
- }
- private static void checkTextField(TextField tx) {
- if (tx.getText().equals("Time to be displayed here")) {
- tx.setText("");
- }
- }
- private static void startTimer(TextField tx) {
- time = Integer.parseInt(tx.getText());
- KeyFrame kf = new KeyFrame(Duration.millis(1000), e -> {
- time--;
- if (time == 0) {
- timeline.stop();
- }
- tx.setText(Integer.toString(time));
- });
- timeline = new Timeline(kf);
- timeline.setCycleCount(Timeline.INDEFINITE);
- timeline.play();
- }
- private static void stopTimer() {
- timeline.pause();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement