RickyS20_

Untitled

Dec 7th, 2020
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.61 KB | None | 0 0
  1.  
  2. import javafx.application.Application;
  3. import javafx.application.Platform;
  4. import javafx.event.ActionEvent;
  5. import javafx.event.EventHandler;
  6. import javafx.geometry.Insets;
  7. import javafx.geometry.Pos;
  8. import javafx.scene.Scene;
  9. import javafx.scene.control.Button;
  10. import javafx.scene.layout.VBox;
  11. import javafx.scene.text.Font;
  12. import javafx.scene.text.Text;
  13. import javafx.stage.Stage;
  14. import java.util.Random;
  15.  
  16. /**
  17. * Write a description of JavaFX class FortuneTeller here.
  18. *
  19. * @author (your name)
  20. * @version (a version number or a date)
  21. */
  22. public class FortuneTeller extends Application
  23. {
  24.     Text fortune = new Text("");
  25.     String[] fortunes = {"Test javaFx"};
  26.  
  27.     @Override
  28.     public void start(Stage stage) throws Exception
  29.         {
  30.             VBox box=new VBox();
  31.             box.setPadding(new Insets(20));
  32.             box.setSpacing(20);
  33.             box.setAlignment(Pos.CENTER);
  34.  
  35.             Text title=new Text("Hello Fortune Teller");
  36.             title.setFont(Font.font("SanSerif",36));
  37.             box.getChildren().add(title);
  38.  
  39.             Button button = new Button("Klik JavaFx");
  40.             box.getChildren().add(button);
  41.  
  42.             button.setOnAction(this::buttonClick);
  43.  
  44.             Scene scene=new Scene(box,500,250);
  45.             stage.setTitle("Hello JavaFx");
  46.             stage.setScene(scene);
  47.             stage.show();
  48.  
  49. }
  50.  
  51. /**
  52. * This will be executed when the button is clicked
  53. * It increments the count by 1
  54. */
  55. private void buttonClick(ActionEvent event)
  56. {
  57. Random rand = new Random();
  58. fortune.setText(fortunes[rand.nextInt(fortunes.length)]);
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment