Programs to implement JavaFX StackPane
Program 1
//java program to demonstrate stack pane
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class App extends Application {
@Override
public void start(Stage s) throws Exception {
//creating button
Button button1 = new Button(“Coding Ninjas");
//creating the stackpane
StackPane stack = new StackPane();
//creating the scene
Scene sc = new Scene(stack,250,250);
stack.getChildren().addAll(button1);
//setting up the scene
s.setScene(sc);
//displaying the result
s.show();
}
public static void main(String[] args) {
launch(args);
}
}

You can also try this code with Online Java Compiler
Run Code
Output

Program 2
import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Sphere;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class App extends Application {
@Override
public void start(Stage stage) {
//Draw rectangle
Rectangle rect = new Rectangle(300,100);
rect.setFill(Color.DARKSLATEBLUE);
rect.setStroke(Color.GRAY);
//Draw Circle
Circle circle = new Circle(300,100,50);
//Create a text
Text text = new Text(“Coding Ninjas");
//Set the font of the text
text.setFont(Font.font(null, FontWeight.BOLD, 15));
//Set the color of the text
text.setFill(Color.WHITE);
//set the position of the text
text.setX(20);
text.setY(50);
//Create a Stackpane
StackPane stackPane = new StackPane();
//Set the margin for the circle
stackPane.setMargin(circle, new Insets(50, 50, 50, 50));
//Retrieve the observable list of the Stack Pane
ObservableList list = stackPane.getChildren();
//Add all the nodes to the pane
list.addAll(rect,circle, text);
//Create a scene object
Scene scene = new Scene(stackPane);
//Set title to the Stage
stage.setTitle("Stack Pane Example");
//Add scene to the stage
stage.setScene(scene);
//Display the contents of the stage
stage.show();
}
public static void main(String args[]){
launch(args);
}
}

You can also try this code with Online Java Compiler
Run Code
Output

Frequently Asked Questions
What purpose does ActionEvent provide in Java?
Java uses for ActionEvent. awt. calls the actionPerformed methods on listeners-a and listener-b to handle the actionPerformed event. It dispatches any registered ActionListener objects with the action events that occur on this menu item.
In JavaFX, how can you tell if a button has been pressed?
When a button is clicked, the subsequent ActionEvent's getTarget() method can be used to obtain the button object. If the button doesn't have a label text, then the button's id will be used to encode when the button was created and can be used to identify it.
What does JavaFX's lambda expression mean?
You can develop an anonymous class that implements a particular functional interface with just one abstract method, using a lambda expression. There is only one abstract method, handle, on the EventHandler interface, which is used to manage JavaFX events.
Conclusion
In this article, we have extensively discussed the introduction to JavaFX StackPane, Syntax of JavaFX StackPane, the constructor of JavaFX StackPane, methods of JavaFX StackPane, and examples of JavaFX StackPane.
After reading about Introduction to JavaFX StackPane, are you not feeling excited to read/explore more articles on the topic of file systems? Don't worry; Coding Ninjas has you covered. If you want to check out articles related to Java refert to these links, Overloading and Overriding static Methods in java, Understanding Association, Aggregation, and Composition in Java, java interview questions, Multiple Inheritance in Java, Multilevel Inheritance in Java.
Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and Algorithms, Competitive Programming, JavaScript, System Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But suppose you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problems, interview experiences, and interview bundle for placement preparations.
Nevertheless, you may consider our paid courses to give your career an edge over others!
Do upvote our blogs if you find them helpful and engaging!
Happy Learning!