JavaFX - 定位Anchorpane



AnchorPane 允許子節點的邊緣與 AnchorPane 邊緣的偏移量對齊。如果 AnchorPane 設定了邊框和/或填充,則偏移量將從這些內邊距的內邊緣測量。

如果我們在應用程式中使用 AnchorPane,其中的節點將與面板保持特定距離。

名為AnchorPane的類,位於包javafx.scene.layout中,表示 AnchorPane。新增節點後,需要從面板的所有方向(頂部、底部、右側和左側)對其設定錨點。為了設定錨點,此類提供了四種方法,分別是 – setBottomAnchor(),setTopAnchor(),setLeftAnchor(),setRightAnchor()。對於這些方法,需要傳遞一個表示錨點的雙精度值。

示例

以下程式是 AnchorPane 佈局的示例。在這個例子中,我們在 AnchorPane 中插入一個旋轉的圓柱體。同時,我們將其從面板的所有方向(頂部、左側、右側、底部)設定在 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 視窗。

AnchorPane
javafx_layout_panes.htm
廣告
© . All rights reserved.