JavaFX中AnchorPane布局

概述

Anchor意思是锚点布局。可以将创建的根节点改变位置。这里主要是介绍一些方法。
首先设置一个按钮作为根节点:

import javafx.application.Application;
import javafx.scene.control.Button;
import javafx.stage.Stage;

public class AnchorPaneTest extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        Button button = new Button("Hello,JavaFX!");
    }
}

然后不使用AnchorPane,直接将按钮放入场景中:

        Scene scene = new Scene(button);
        Stage stage = new Stage();
        stage.setScene(scene);
        stage.setWidth(500);
        stage.setHeight(500);
        stage.show();

运行结果:
在这里插入图片描述
可以看见按钮将场景占满了。当按钮用AnchorPane布局之后:

    public void start(Stage primaryStage) throws Exception {
        Button button = new Button("Hello,JavaFX!");
        AnchorPane pane = new AnchorPane(button);
        Scene scene = new Scene(pane);
        Stage stage = new Stage();
        stage.setScene(scene);
        stage.setWidth(500);
        stage.setHeight(500);
        stage.show();
    }

运行结果:
在这里插入图片描述
按钮变小了,并且在没有给按钮设置位置的情况下,默认位置为(0,0)。

方法

如果我们为按钮设置了坐标,之后会在相应的坐标显示。用这个方法增加节点时,可以一次增加多个:

        Button button = new Button("Hello,JavaFX!");
        Button button1 = new Button("Happy!");
        AnchorPane pane = new AnchorPane(button,button1);

当然,还有另外的方法,方便随时加节点,add()一次只能加一个,addAll()可以一次增加多个:

        pane.getChildren().add(button);
        pane.getChildren().addAll(button,button1);

AnchorPane还可以为节点定位,使节点相对于窗口的上下左右的位置不变:

        AnchorPane.setTopAnchor(button,100.0);
        AnchorPane.setLeftAnchor(button,150.0);

其他两边的方法是类似的,运行结果:
在这里插入图片描述
设置之后,无论怎么放大缩小窗口,button相对于窗口的上左的距离是不变的。
既然有set,那么get 也要安排上,就是获取关于设置的相对距离的数值:

        System.out.println(AnchorPane.getTopAnchor(button));

结果就是输出设置的100.0。
AnchorPane中有一个方法叫clearConstraints(Node child)作用是清除作用在这个节点上的定位束缚。

        AnchorPane.clearConstraints(button);

运行后会发现,这个按钮回到了原来的位置,并且不再有定位的束缚。

总结

如果有时间,看了API,会发现AnchorPane继承了Pane,Region,Parent,Node,Object的好多方法,尤其是Node,压根数不过来。所以像我这样的初学javafx的,建议先看一些超类,不然看这种,继承了很多方法,还不知道其所以然。

### JavaFX AnchorPane 的使用方法及示例 #### 什么是 AnchorPane? `AnchorPane` 是 JavaFX 中的一种布局容器,允许开发者通过设置控件相对于其边界的距离来精确定位子节点的位置。这种灵活性使得 `AnchorPane` 非常适合需要精确控制 UI 布局的场景。 --- #### 如何定义和配置 AnchorPane? 可以通过代码或者 FXML 文件定义 `AnchorPane` 并为其添加子节点。以下是两种方式的具体实现: 1. **通过代码创建 AnchorPane** 下面是一个简单的例子,展示如何在代码中创建 `AnchorPane` 并向其中添加一个 `Label` 控件[^2]。 ```java import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.layout.AnchorPane; import javafx.stage.Stage; public class AnchorPaneExample extends Application { @Override public void start(Stage primaryStage) { // 创建一个新的 AnchorPane 容器 AnchorPane anchorPane = new AnchorPane(); // 添加一个 Label 到 AnchorPane Label label = new Label("This is an AnchorPane Example"); // 设置 Label 距离顶部和左侧的距离 AnchorPane.setTopAnchor(label, 50.0); AnchorPane.setLeftAnchor(label, 50.0); // 将 Label 添加到 AnchorPane anchorPane.getChildren().add(label); // 创建 Scene 并设置舞台 Scene scene = new Scene(anchorPane, 400, 300); primaryStage.setTitle("AnchorPane Demo"); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } } ``` 2. **通过 FXML 文件创建 AnchorPane** 如果希望使用 FXML 来设计界面,则可以在 XML 文件中声明 `AnchorPane` 和它的子节点,并指定它们的锚定位置[^1]。 ```xml <?import javafx.scene.control.Label?> <?import javafx.scene.layout.AnchorPane?> <AnchorPane xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml"> <!-- 添加一个 Label --> <children> <Label text="This is a Label inside AnchorPane" AnchorPane.topAnchor="50.0" AnchorPane.leftAnchor="50.0"/> </children> </AnchorPane> ``` --- #### 锚定点的属性说明 `AnchorPane` 提供了四个主要的静态方法用于设置子节点的锚定点: - `setTopAnchor(Node node, Double value)`:设置节点距顶部的距离。 - `setBottomAnchor(Node node, Double value)`:设置节点距底部的距离。 - `setLeftAnchor(Node node, Double value)`:设置节点距左边的距离。 - `setRightAnchor(Node node, Double value)`:设置节点距右边的距离。 这些方法可以单独或组合使用,从而灵活调整子节点的位置。 --- #### AnchorPane 的适用场景 由于 `AnchorPane` 不会自动调整子节点大小或重新排列它们,因此它非常适合以下情况: - 当需要完全手动控制子节点的位置时。 - 设计固定尺寸的复杂用户界面组件时。 然而,在大多数情况下,如果需要动态适应窗口大小变化的功能,推荐使用其他更高级别的布局管理器(如 `GridPane`, `HBox`, 或者 `VBox`)[^4]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值