JavaFX - 布局锚点

  • 简述

    锚窗格允许将子节点的边缘锚定到与锚窗格边缘的偏移量。如果锚点窗格设置了边框和/或填充,则将从这些插图的内边缘开始测量偏移量。
    如果我们在应用程序中使用 Anchor 窗格,其中的节点将锚定在距窗格特定距离的位置。
    类名为 AnchorPane 包裹的 javafx.scene.layout代表锚窗格。添加节点后,您需要从窗格的所有方向(顶部、底部、右侧和左侧)为其设置一个锚点。为了设置锚点,这个类提供了四种方法,它们是——setBottomAnchor(), setTopAnchor(), setLeftAnchor(), setRightAnchor(). 对于这些方法,您需要传递一个表示锚点的双精度值。
  • 例子

    以下程序是 Anchor Pane 布局的示例。在这里,我们在锚定窗格中插入一个旋转圆柱体。同时,我们将其设置为从各个方向(顶部、左侧、右侧、底部)距窗格 50 个单位的距离。
    将此代码保存在名称为的文件中 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); 
       } 
    }
    
    使用以下命令从命令提示符编译并执行保存的 java 文件。
    
    javac AnchorPaneExample.java 
    java AnchorPaneExample
    
    执行时,上述程序会生成一个 JavaFX 窗口,如下所示。
    锚定面板