Add StackPane to a Scene and then add Scene to Stage : StackPane « JavaFX « Java






Add StackPane to a Scene and then add Scene to Stage

 

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
 
public class Main extends Application {
    public static void main(String[] args) {
        launch(args);
    }
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Hello World!");
        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
            }
        });
        
        StackPane root = new StackPane();
        root.getChildren().add(btn);
        primaryStage.setScene(new Scene(root, 300, 250));
        primaryStage.show();
    }
}

   
  








Related examples in the same category

1.Add Control to StackPane
2.StackPane based wizard
3.Using StackPane to hold controls
4.Align the title Label at the bottom-center of the stackpane
5.Create an 8 pixel margin around a listview in the stackpane
6.Adding components to StackPane