JavaFX - Stackpane de disposition

Si nous utilisons le volet de pile, les nœuds sont disposés les uns sur les autres, comme dans la pile. Le nœud ajouté en premier est placé au bas de la pile et le nœud suivant est placé dessus.

La classe nommée StackPane du forfait javafx.scene.layoutreprésente le StackPane. Cette classe contient une seule propriété appelée alignement. Cette propriété représente l'alignement des nœuds dans le volet de pile.

En plus de ceux-ci, cette classe fournit également une méthode nommée setMargin(). Cette méthode est utilisée pour définir la marge du nœud dans le volet de pile.

Exemple

Le programme suivant est un exemple de StackPanedisposition. En cela, nous insérons un cercle, une sphère et un texte dans le même ordre.

Enregistrez ce code dans un fichier avec le nom StackPaneExample.java.

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.Sphere;
 
import javafx.scene.text.Font; 
import javafx.scene.text.FontWeight; 
import javafx.scene.text.Text; 
import javafx.stage.Stage; 
         
public class StackPaneExample extends Application { 
   @Override 
   public void start(Stage stage) {                     
      //Drawing a Circle 
      Circle circle = new Circle(300, 135, 100); 
      circle.setFill(Color.DARKSLATEBLUE); 
      circle.setStroke(Color.BLACK);
      
      //Drawing Sphere 
      Sphere sphere = new Sphere(50); 
       
      //Creating a text 
      Text text = new Text("Hello how are you"); 
      
      //Setting the font of the text 
      text.setFont(Font.font(null, FontWeight.BOLD, 15));     
      
      //Setting the color of the text 
      text.setFill(Color.CRIMSON); 
      
      //setting the position of the text 
      text.setX(20); 
      text.setY(50);       
       
      //Creating a Stackpane 
      StackPane stackPane = new StackPane(); 
      
      //Setting the margin for the circle 
      stackPane.setMargin(circle, new Insets(50, 50, 50, 50));       
       
      //Retrieving the observable list of the Stack Pane 
      ObservableList list = stackPane.getChildren(); 
      
      //Adding all the nodes to the pane 
      list.addAll(circle, sphere, text); 
        
      //Creating a scene object 
      Scene scene = new Scene(stackPane);  
      
      //Setting title to the Stage 
      stage.setTitle("Stack Pane Example"); 
         
      //Adding scene to the stage 
      stage.setScene(scene); 
         
      //Displaying the contents of the stage 
      stage.show();  
   }
   public static void main(String args[]){ 
      launch(args); 
   } 
}

Compilez et exécutez le fichier java enregistré à partir de l'invite de commande à l'aide des commandes suivantes.

javac StackPaneExample.java 
java StackPaneExample

Lors de l'exécution, le programme ci-dessus génère une fenêtre JavaFX comme indiqué ci-dessous.