- 将子节点锚定到容器的边界上,指定子节点相对于 AnchorPane 的四个边界(上、下、左、右)的距离
- 适合宽高固定的一些表单
- 如果允许最大化,拖动大小,需要自己计算子节点的定位
常用属性
padding
容器边缘与其子节点之间的距离
anchorPane.setPadding(new Insets(5, 10, 5, 10));
实现方式
Java
public static AnchorPane demo1() {
// 创建AnchorPane
AnchorPane anchorPane = new AnchorPane();
// 内边距
anchorPane.setPadding(new Insets(10, 10, 10, 10));
// 用户名
Label label1 = new Label("用户名:");
AnchorPane.setTopAnchor(label1, 30D);
AnchorPane.setLeftAnchor(label1, 90D);
anchorPane.getChildren().add(label1);
TextField textField1 = new TextField();
AnchorPane.setTopAnchor(textField1, 30D);
AnchorPane.setLef