JavaFX - Mise en page Anchorpane

Le volet d'ancrage permet aux arêtes des nœuds enfants d'être ancrées à un décalage par rapport aux arêtes du volet d'ancrage. Si le volet d'ancrage a une bordure et / ou un jeu de remplissage, les décalages seront mesurés à partir du bord intérieur de ces encarts.

Si nous utilisons un volet d'ancrage dans notre application, les nœuds qu'il contient sont ancrés à une distance particulière du volet.

La classe nommée AnchorPane du forfait javafx.scene.layoutreprésente le volet d'ancrage. Une fois qu'un nœud a été ajouté, vous devez lui définir une ancre à partir des limites du volet dans toutes les directions (haut, bas, droite et gauche). Pour définir l'ancre, cette classe fournit quatre méthodes, qui sont -setBottomAnchor(), setTopAnchor(), setLeftAnchor(), setRightAnchor(). Pour ces méthodes, vous devez passer une valeur double représentant l'ancre.

Exemple

Le programme suivant est un exemple de la disposition du volet d'ancrage. En cela, nous insérons un cylindre rotatif dans une vitre d'ancrage. En même temps, nous le réglons à une distance de 50 unités du volet de toutes les directions (Haut, Gauche, Droite, Bas).

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

import javafx.animation.RotateTransition; 
import javafx.collections.ObservableList; 
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane; 
import javafx.scene.paint.Color; 
import javafx.scene.paint.PhongMaterial; 
import javafx.scene.shape.Cylinder; 
import javafx.scene.transform.Rotate; 
import javafx.stage.Stage; 
import javafx.util.Duration;          

public class AnchorPaneExample extends Application { 
   @Override 
   public void start(Stage stage) {                     
      //Drawing a Cylinder 
      Cylinder cylinder = new Cylinder();        
      
      //Setting the properties of the Cylinder 
      cylinder.setHeight(180.0f); 
      cylinder.setRadius(100.0f);     
      
      //Preparing the phong material of type diffuse color 
      PhongMaterial material = new PhongMaterial();  
      material.setDiffuseColor(Color.BLANCHEDALMOND); 
      
      //Setting the diffuse color material to Cylinder5 
      cylinder.setMaterial(material);  
       
      //Setting rotation transition for the cylinder 
      RotateTransition rotateTransition = new RotateTransition(); 
      
      //Setting the duration for the transition 
      rotateTransition.setDuration(Duration.millis(1000)); 
      
      //Setting the node for the transition 
      rotateTransition.setNode(cylinder);       
      
      //Setting the axis of the rotation 
      rotateTransition.setAxis(Rotate.X_AXIS);  
      
      //Setting the angle of the rotation 
      rotateTransition.setByAngle(360);
      
      //Setting the cycle count for the transition 
      rotateTransition.setCycleCount(RotateTransition.INDEFINITE); 
      
      //Setting auto reverse value to false 
      rotateTransition.setAutoReverse(false); 
      
      //playing the animation 
      rotateTransition.play(); 
      
      //Creating an Anchor Pane  
      AnchorPane anchorPane = new AnchorPane();  
       
      //Setting the anchor to the cylinder      
      AnchorPane.setTopAnchor(cylinder, 50.0); 
      AnchorPane.setLeftAnchor(cylinder, 50.0); 
      AnchorPane.setRightAnchor(cylinder, 50.0); 
      AnchorPane.setBottomAnchor(cylinder, 50.0); 
       
      //Retrieving the observable list of the Anchor Pane 
      ObservableList list = anchorPane.getChildren();  
      
      //Adding cylinder to the pane 
      list.addAll(cylinder);        
         
      //Creating a scene object 
      Scene scene = new Scene(anchorPane);  
      
      //Setting title to the Stage 
      stage.setTitle("Anchor 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 AnchorPaneExample.java 
java AnchorPaneExample

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