Guest User

Untitled

a guest
Nov 27th, 2019
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 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. * JavaFX application that tells your fortune.
  18. *
  19. * @author Kelly Lougheed
  20. * @version 1.0
  21. */
  22. public class FortuneTeller extends Application
  23. {
  24. Text fortune = new Text("");
  25. String[] fortunes = {"You will lead a long and happy life",
  26. "You will become a famous Hollywood actor",
  27. "You will win the lottery but then lose everything"};
  28.  
  29. @Override
  30. public void start(Stage stage) throws Exception
  31. {
  32. VBox box = new VBox();
  33. box.setPadding(new Insets(20));
  34. box.setSpacing(20);
  35. box.setAlignment(Pos.CENTER);
  36.  
  37. Text title = new Text("Fortune Teller");
  38. title.setFont(Font.font("SanSerif", 36));
  39.  
  40. box.getChildren().add(title);
  41.  
  42. fortune.setFont(Font.font("SanSerif", 18));
  43.  
  44. box.getChildren().add(fortune);
  45.  
  46. Button button = new Button("New Fortune");
  47. box.getChildren().add(button);
  48.  
  49. button.setOnAction(this::buttonClick);
  50.  
  51. Scene scene = new Scene(box, 500, 250);
  52. stage.setTitle("Fortune Teller");
  53. stage.setScene(scene);
  54. stage.show();
  55. }
  56.  
  57. private void buttonClick(ActionEvent event)
  58. {
  59. Random rand = new Random();
  60. fortune.setText(fortunes[rand.nextInt(fortunes.length)]);
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment