HIDE NAV

JavaFX Hello World Button

A basic JavaFX program to test that your JavaFX toolchain is okay and show the essential elements of a simple GUI (Graphic User Interface).

Main.java


package ceccs;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class MyApp extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        VBox root = new VBox();
        root.setAlignment(Pos.CENTER);
        root.setSpacing(30);

        Label lbl = new Label("---");
        Button btn = new Button("Say Hello");
        btn.setOnAction((actionEvent) -> {
            lbl.setText("Hello World!");
        });

        root.getChildren().addAll(lbl,btn);

        Scene scn = new Scene(root);
        primaryStage.setWidth(500);
        primaryStage.setHeight(300);
        primaryStage.setScene(scn);
        primaryStage.setTitle("JFX Hello World");
        primaryStage.show();
    }

}