|
| 1 | +import javafx.application.Application; |
| 2 | +import javafx.event.ActionEvent; |
| 3 | +import javafx.event.EventHandler; |
| 4 | +import javafx.scene.Scene; |
| 5 | +import javafx.scene.control.Button; |
| 6 | +import javafx.scene.layout.StackPane; |
| 7 | +import javafx.stage.Stage; |
| 8 | + |
| 9 | +public class Main extends Application { |
| 10 | + |
| 11 | + //Calls launch in superclass |
| 12 | + //This sets up program as JavaFX application then calls start() |
| 13 | + public static void main(String[] args) { |
| 14 | + launch(args); |
| 15 | + } |
| 16 | + |
| 17 | + //Every JavaFX program needs this method |
| 18 | + //Only parameter is stage (the main window) |
| 19 | + @Override |
| 20 | + public void start(Stage primaryStage) { |
| 21 | + |
| 22 | + primaryStage.setTitle("Hello World!"); |
| 23 | + Button btn = new Button(); |
| 24 | + btn.setText("Hey baby"); |
| 25 | + |
| 26 | + |
| 27 | + //When button is clicked, an action occurs |
| 28 | + btn.setOnAction(new EventHandler<ActionEvent>() { |
| 29 | + @Override |
| 30 | + public void handle(ActionEvent event) { |
| 31 | + System.out.println("Hello World!"); |
| 32 | + } |
| 33 | + }); |
| 34 | + |
| 35 | + //A layout pane is how you want everything to be positioned and displayed |
| 36 | + //Will make a lot more sense when we have more than one item |
| 37 | + StackPane layout = new StackPane(); |
| 38 | + layout.getChildren().add(btn); |
| 39 | + |
| 40 | + //Add a scene to the stage (content to the window) |
| 41 | + Scene scene = new Scene(layout, 300, 250); |
| 42 | + primaryStage.setScene(scene); |
| 43 | + primaryStage.show(); |
| 44 | + } |
| 45 | + |
| 46 | +} |