JavaFX | StackPane Class - GeeksforGeeks

13 Sep, 2018
StackPane class is a part of JavaFX. StackPane class lays out its children in form of a stack. The new node is placed on the top of the previous node in a StackPane. StackPane class inherits Pane Class.
Constructors of the class:
StackPane(): Creates a new empty StackPane.StackPane(Node... c): Creates a new StackPne with specified nodes.
StackPane(): Creates a new empty StackPane.
StackPane(Node... c): Creates a new StackPne with specified nodes.
Commonly Used Methods:
Below programs illustrate the use of StackPane Class:
Java Program to create a StackPane, add circle, label, rectangle and add it to the stage: In this program we are creating a Label named label, a Rectangle named rectangle and a Circle named circle. Then set the font of the StackPane using the setFont() function. Now set the fill of the rectangle and circle using the setFill() function. We will then create a StackPane named stack_pane and add rectangle, circle and label. Create a scene and add the stack_pane to the scene. Add this scene to the stage and call the show() function to display the final results.// Java Program to create a StackPane,// add circle, label, rectangle// and add it to the stageimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.*;import javafx.scene.layout.*;import javafx.stage.Stage;import javafx.event.ActionEvent;import javafx.scene.paint.*;import javafx.scene.canvas.*;import javafx.scene.text.*;import javafx.scene.Group;import javafx.scene.shape.*;  public class StackPane_1 extends Application {      // launch the application    public void start(Stage stage)    {          try {              // set title for the stage            stage.setTitle("StackPane");              // create a label            Label label = new Label("this is StackPane example");              // set Font for label            label.setFont(new Font(30));              // create a circle            Circle circle = new Circle(100, 100, 70);              // set fill for  the circle            circle.setFill(Color.RED);              // create Rectangle            Rectangle rectangle = new Rectangle(100, 100, 180, 160);              // set fill for rectangle            rectangle.setFill(Color.BLUE);              // create a stack pane            StackPane stack_pane = new StackPane(rectangle, circle, label);              // create a scene            Scene scene = new Scene(stack_pane, 400, 300);              // set the scene            stage.setScene(scene);              stage.show();        }          catch (Exception e) {              System.out.println(e.getMessage());        }    }      // Main Method    public static void main(String args[])    {          // launch the application        launch(args);    }}Output:Java Program to create a StackPane, add the circle, label, rectangle and then set the alignment of the StackPane and add it to the stage: In this program we are creating a Label named label, a Rectangle named rectangle and a Circle named circle. Then set the font of the StackPane using the setFont() function. Set fill of the rectangle and circle using the setFill() function. Now create a StackPane named stack_pane and add rectangle, circle, and label. Set the alignment of the stack_pane using setAlignment() function. Create a scene and add the stack_pane to the scene. Finally add this scene to the stage and call the show() function to display the results.// Java Program to create a StackPane, // add the circle, label, rectangle and// then set the alignment of the StackPane// and add it to the stageimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.*;import javafx.scene.layout.*;import javafx.stage.Stage;import javafx.geometry.*;import javafx.scene.paint.*;import javafx.scene.canvas.*;import javafx.scene.text.*;import javafx.scene.Group;import javafx.scene.shape.*;  public class StackPane_2 extends Application {      // launch the application    public void start(Stage stage)    {          try {              // set title for the stage            stage.setTitle("StackPane");              // create a label            Label label = new Label("this is StackPane example");              // set Font for label            label.setFont(new Font(30));              // create a circle            Circle circle = new Circle(100, 100, 70);              // set fill for  the circle            circle.setFill(Color.RED);              // create Rectangle            Rectangle rectangle = new Rectangle(100, 100, 180, 160);              // set fill for rectangle            rectangle.setFill(Color.BLUE);              // create a stack pane            StackPane stack_pane = new StackPane(rectangle, circle, label);              // set alignement for the stack pane            stack_pane.setAlignment(Pos.TOP_CENTER);              // create a scene            Scene scene = new Scene(stack_pane, 400, 300);              // set the scene            stage.setScene(scene);              stage.show();        }          catch (Exception e) {              System.out.println(e.getMessage());        }    }      // Main Method    public static void main(String args[])    {          // launch the application        launch(args);    }}Output:Note: The above programs might not run in an online IDE please use an offline compiler.Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/StackPane.htmlMy Personal Notes
arrow_drop_upSave
Java Program to create a StackPane, add circle, label, rectangle and add it to the stage: In this program we are creating a Label named label, a Rectangle named rectangle and a Circle named circle. Then set the font of the StackPane using the setFont() function. Now set the fill of the rectangle and circle using the setFill() function. We will then create a StackPane named stack_pane and add rectangle, circle and label. Create a scene and add the stack_pane to the scene. Add this scene to the stage and call the show() function to display the final results.// Java Program to create a StackPane,// add circle, label, rectangle// and add it to the stageimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.*;import javafx.scene.layout.*;import javafx.stage.Stage;import javafx.event.ActionEvent;import javafx.scene.paint.*;import javafx.scene.canvas.*;import javafx.scene.text.*;import javafx.scene.Group;import javafx.scene.shape.*;  public class StackPane_1 extends Application {      // launch the application    public void start(Stage stage)    {          try {              // set title for the stage            stage.setTitle("StackPane");              // create a label            Label label = new Label("this is StackPane example");              // set Font for label            label.setFont(new Font(30));              // create a circle            Circle circle = new Circle(100, 100, 70);              // set fill for  the circle            circle.setFill(Color.RED);              // create Rectangle            Rectangle rectangle = new Rectangle(100, 100, 180, 160);              // set fill for rectangle            rectangle.setFill(Color.BLUE);              // create a stack pane            StackPane stack_pane = new StackPane(rectangle, circle, label);              // create a scene            Scene scene = new Scene(stack_pane, 400, 300);              // set the scene            stage.setScene(scene);              stage.show();        }          catch (Exception e) {              System.out.println(e.getMessage());        }    }      // Main Method    public static void main(String args[])    {          // launch the application        launch(args);    }}Output:
// Java Program to create a StackPane,// add circle, label, rectangle// and add it to the stageimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.*;import javafx.scene.layout.*;import javafx.stage.Stage;import javafx.event.ActionEvent;import javafx.scene.paint.*;import javafx.scene.canvas.*;import javafx.scene.text.*;import javafx.scene.Group;import javafx.scene.shape.*;  public class StackPane_1 extends Application {      // launch the application    public void start(Stage stage)    {          try {              // set title for the stage            stage.setTitle("StackPane");              // create a label            Label label = new Label("this is StackPane example");              // set Font for label            label.setFont(new Font(30));              // create a circle            Circle circle = new Circle(100, 100, 70);              // set fill for  the circle            circle.setFill(Color.RED);              // create Rectangle            Rectangle rectangle = new Rectangle(100, 100, 180, 160);              // set fill for rectangle            rectangle.setFill(Color.BLUE);              // create a stack pane            StackPane stack_pane = new StackPane(rectangle, circle, label);              // create a scene            Scene scene = new Scene(stack_pane, 400, 300);              // set the scene            stage.setScene(scene);              stage.show();        }          catch (Exception e) {              System.out.println(e.getMessage());        }    }      // Main Method    public static void main(String args[])    {          // launch the application        launch(args);    }}
Output:
Java Program to create a StackPane, add the circle, label, rectangle and then set the alignment of the StackPane and add it to the stage: In this program we are creating a Label named label, a Rectangle named rectangle and a Circle named circle. Then set the font of the StackPane using the setFont() function. Set fill of the rectangle and circle using the setFill() function. Now create a StackPane named stack_pane and add rectangle, circle, and label. Set the alignment of the stack_pane using setAlignment() function. Create a scene and add the stack_pane to the scene. Finally add this scene to the stage and call the show() function to display the results.// Java Program to create a StackPane, // add the circle, label, rectangle and// then set the alignment of the StackPane// and add it to the stageimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.*;import javafx.scene.layout.*;import javafx.stage.Stage;import javafx.geometry.*;import javafx.scene.paint.*;import javafx.scene.canvas.*;import javafx.scene.text.*;import javafx.scene.Group;import javafx.scene.shape.*;  public class StackPane_2 extends Application {      // launch the application    public void start(Stage stage)    {          try {              // set title for the stage            stage.setTitle("StackPane");              // create a label            Label label = new Label("this is StackPane example");              // set Font for label            label.setFont(new Font(30));              // create a circle            Circle circle = new Circle(100, 100, 70);              // set fill for  the circle            circle.setFill(Color.RED);              // create Rectangle            Rectangle rectangle = new Rectangle(100, 100, 180, 160);              // set fill for rectangle            rectangle.setFill(Color.BLUE);              // create a stack pane            StackPane stack_pane = new StackPane(rectangle, circle, label);              // set alignement for the stack pane            stack_pane.setAlignment(Pos.TOP_CENTER);              // create a scene            Scene scene = new Scene(stack_pane, 400, 300);              // set the scene            stage.setScene(scene);              stage.show();        }          catch (Exception e) {              System.out.println(e.getMessage());        }    }      // Main Method    public static void main(String args[])    {          // launch the application        launch(args);    }}Output:Note: The above programs might not run in an online IDE please use an offline compiler.Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/StackPane.htmlMy Personal Notes
arrow_drop_upSave
// Java Program to create a StackPane, // add the circle, label, rectangle and// then set the alignment of the StackPane// and add it to the stageimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.*;import javafx.scene.layout.*;import javafx.stage.Stage;import javafx.geometry.*;import javafx.scene.paint.*;import javafx.scene.canvas.*;import javafx.scene.text.*;import javafx.scene.Group;import javafx.scene.shape.*;  public class StackPane_2 extends Application {      // launch the application    public void start(Stage stage)    {          try {              // set title for the stage            stage.setTitle("StackPane");              // create a label            Label label = new Label("this is StackPane example");              // set Font for label            label.setFont(new Font(30));              // create a circle            Circle circle = new Circle(100, 100, 70);              // set fill for  the circle            circle.setFill(Color.RED);              // create Rectangle            Rectangle rectangle = new Rectangle(100, 100, 180, 160);              // set fill for rectangle            rectangle.setFill(Color.BLUE);              // create a stack pane            StackPane stack_pane = new StackPane(rectangle, circle, label);              // set alignement for the stack pane            stack_pane.setAlignment(Pos.TOP_CENTER);              // create a scene            Scene scene = new Scene(stack_pane, 400, 300);              // set the scene            stage.setScene(scene);              stage.show();        }          catch (Exception e) {              System.out.println(e.getMessage());        }    }      // Main Method    public static void main(String args[])    {          // launch the application        launch(args);    }}
Output:
Note: The above programs might not run in an online IDE please use an offline compiler.
Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/StackPane.html
JavaFX
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Constructors in Java
Exceptions in Java
Functional Interfaces in Java
Different ways of Reading a text file in Java
Generics in Java
Introduction to Java
Comparator Interface in Java with Examples
Internal Working of HashMap in Java
Strings in Java