Javafx 简明教程

JavaFX - Anchorpane Layout

AnchorPane Layout in JavaFX

Anchor Pane 允许将子节点边缘锚定在相对于 anchor 窗口边缘的指定偏移量处。它确保节点与布局窗格的边缘保持固定距离。在 JavaFX 中,anchor 窗口由名称为 AnchorPane 的类表示,该类是 javafx.scene.layout 包的一部分。

假设我们有一个节点,我们需要从窗格的边界在所有方向(顶部、底部、右侧和左侧)设置一个 anchor。为了设置 anchor,AnchorPane 类提供了四种内置方法,即 setBottomAnchor()setTopAnchor()setLeftAnchor()setRightAnchor() 。这些方法接受一个表示 anchor 的 double 值。下图对此进行了说明 −

anchorpane sample

Note that 如果 anchor 窗口设置了边框和/或内边距,则会根据这些内边距的内边缘计算偏移量。

AnchorPane 类的构造函数如下 −

  1. AnchorPane() - 构造一个空 AnchorPane 布局。

  2. AnchorPane(Node childNodes) - 使用指定节点创建一个新的 AnchorPane 布局。

Example

以下程序是 Anchor Pane 布局的示例。在此处,我们在锚点窗格中插入一个旋转圆柱体。将此代码保存在名为 AnchorPaneExample.java 的文件中。

import javafx.animation.RotateTransition;
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);

      //Adding cylinder to the pane
      anchorPane.getChildren().addAll(cylinder);

      //Creating a scene object
      Scene scene = new Scene(anchorPane, 400, 300);

      //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 --module-path %PATH_TO_FX% --add-modules javafx.controls AnchorPaneExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls AnchorPaneExample

执行后,上述程序会生成如下所示的 JavaFX 窗口。

anchorpane output