AnchorPane将子节点的边缘锚定到距锚窗格边缘的偏移处。如果锚窗格设置了边框或填充,则偏移量将从这些插图的内边缘开始测量。
类的构造函数:
- AnchorPane():创建一个新的 AnchorPane。
 - AnchorPane(Node… c):创建具有指定节点的AnchorPane。
 
常用方法:
| 方法 | 解释 | 
|---|---|
| getBottomAnchor(节点 c) | 返回孩子的底部锚点。 | 
| getLeftAnchor(节点 c) | 返回孩子的左码头。 | 
| getRightAnchor(节点 c) | 返回孩子的右翼。 | 
| getTopAnchor(节点 c) | 返回孩子的顶部航点。 | 
| setBottomAnchor(节点 c, 双 v) | 设置孩子的底部锚点。 | 
| setLeftAnchor(节点 c,双 v) | 设置孩子的左航。 | 
| setRightAnchor(节点 c, 双 v) | 设置孩子的航标。 | 
| setTopAnchor(节点 c, 双 v) | 设置孩子的顶部航点。 | 
让我们创建一个 JavaFX 示例,该示例使用AnchorPane和HBox将两个按钮定位到窗口的右下角。
import javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.control.Label;import javafx.scene.layout.AnchorPane;import javafx.scene.layout.HBox;import javafx.stage.Stage;public class Main extends Application {public void start(Stage stage) {stage.setTitle("AnchorPane");HBox hBox = new HBox();Button b1 = new Button("Login");Button b2 = new Button("Regist");hBox.getChildren().addAll(b1,b2);AnchorPane anchor_pane = new AnchorPane();anchor_pane.getChildren().addAll(hBox);AnchorPane.setTopAnchor(hBox, 50.0);AnchorPane.setLeftAnchor(hBox, 10.0);Scene scene = new Scene(anchor_pane, 400, 300);stage.setScene(scene);stage.show();}public static void main(String args[]) {launch(args);}}

import javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.control.Label;import javafx.scene.layout.AnchorPane;import javafx.scene.layout.HBox;import javafx.stage.Stage;public class Main extends Application {public void start(Stage stage) {stage.setTitle("AnchorPane");Button b1 = new Button("Login");AnchorPane anchor_pane = new AnchorPane();anchor_pane.getChildren().addAll(b1);AnchorPane.setTopAnchor(b1, 50D);AnchorPane.setLeftAnchor(b1,50D);Scene scene = new Scene(anchor_pane, 400, 300);stage.setScene(scene);stage.show();}public static void main(String args[]) {launch(args);}}

