Advertisement
mnaufaldillah

FortuneTeller Tugas 8

Dec 13th, 2020
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.18 KB | None | 0 0
  1.  
  2.  
  3. import javafx.application.Application;
  4. import javafx.application.Platform;
  5. import javafx.event.ActionEvent;
  6. import javafx.event.EventHandler;
  7. import javafx.geometry.Insets;
  8. import javafx.geometry.Pos;
  9. import javafx.scene.Scene;
  10. import javafx.scene.control.Button;
  11. import javafx.scene.layout.VBox;
  12. import javafx.scene.text.Font;
  13. import javafx.scene.text.Text;
  14. import javafx.stage.Stage;
  15. import java.util.Random;
  16.  
  17. /**
  18.  * Write a description of JavaFX class FortuneTeller here.
  19.  *
  20.  * @author Muhammad Naufaldillah
  21.  * @version 13 Desember 2020
  22.  */
  23. public class FortuneTeller extends Application
  24. {
  25.     Text fortune = new Text("");
  26.     String[] fortunes = {"Test javaFx", "Lulus dengan IPK 4.0",
  27.                          "Kerja di perusahaan Start-up", "Liburan keliling dunia", "Menjadi presiden Indonesia"};
  28.  
  29.     /**
  30.      * The start method is the main entry point for every JavaFX application.
  31.      * It is called after the init() method has returned and after
  32.      * the system is ready for the application to begin running.
  33.      *
  34.      * @param  stage the primary stage for this application.
  35.      */
  36.     @Override
  37.     public void start(Stage stage) throws Exception
  38.     {
  39.         VBox box = new VBox();
  40.         box.setPadding(new Insets(20));
  41.         box.setSpacing(20);
  42.         box.setAlignment(Pos.CENTER);
  43.        
  44.         Text title = new Text("Fortune Teller");
  45.         title.setFont(Font.font("Arial", 36));
  46.         box.getChildren().add(title);
  47.        
  48.         fortune.setFont(Font.font("Arial", 18));
  49.         box.getChildren().add(fortune);
  50.        
  51.         Button button = new Button("Buka Ramalan anda.");
  52.         box.getChildren().add(button);
  53.  
  54.         button.setOnAction(this::buttonClick);
  55.        
  56.         Scene scene = new Scene(box, 500, 250);
  57.         stage.setTitle("Fortune Teller");
  58.         stage.setScene(scene);
  59.         stage.show();
  60.        
  61.        
  62.     }
  63.  
  64.     /**
  65.      * This will be executed when the button is clicked
  66.      * It increments the count by 1
  67.      */
  68.     private void buttonClick(ActionEvent event)
  69.     {
  70.         Random rand = new Random();
  71.         fortune.setText(fortunes[rand.nextInt(fortunes.length)]);
  72.     }
  73. }
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement