JavaFX布局-AnchorPane

JavaFX布局-AnchorPane

  • 将子节点锚定到容器的边界上,指定子节点相对于 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.setLeftAnchor(textField1, 180D);
        anchorPane.getChildren().add(textField1);

        // 密码
        Label label2 = new Label("密码:");
        AnchorPane.setTopAnchor(label2, 70D);
        AnchorPane.setLeftAnchor(label2, 90D);
        anchorPane.getChildren().add(label2);

        // 密码
        PasswordField passwordField = new PasswordField();
        AnchorPane.setTopAnchor(passwordField, 70D);
        AnchorPane.setLeftAnchor(passwordField, 180D);
        anchorPane.getChildren().add(passwordField);

        return anchorPane;
    }

fxml

<AnchorPane prefHeight="400" prefWidth="600" xmlns="http://javafx.com/javafx/17.0.2-ea"
            xmlns:fx="http://javafx.com/fxml/1">
    <padding>
        <Insets left="10" top="5" right="10" bottom="5"/>
    </padding>
    <children>
        <Label layoutX="90" layoutY="30" text="用户名:"/>
        <TextField layoutX="180" layoutY="30"/>

        <Label layoutX="90" layoutY="70" text="密码:"/>
        <PasswordField layoutX="180" layoutY="70"/>
    </children>
</AnchorPane>
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值