javafx:AnchorPane设置子容器和父容器同等大小

15 篇文章 1 订阅

 

AnchorPane 布局面板允许你将控件停靠在窗口的上下左右位置,即使窗口改变了大小,控件也会保持它们停靠的位置不变。同一个控件可以设置多个停靠属性,同一个位置可以停靠多个控件。

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class AnchorPaneExample extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Anchor Pane Example!");

        AnchorPane anchorPane = new AnchorPane();
        
        Button topBtn = new Button("Top Button");
        Button bottomBtn = new Button("Bottom Button");
        Button leftBtn = new Button("Left Button");
        Button rightBtn = new Button("Right Button");
        
        anchorPane.getChildren().addAll(topBtn,bottomBtn,leftBtn,rightBtn);
        AnchorPane.setBottomAnchor(bottomBtn, 8.0);
        AnchorPane.setRightAnchor(rightBtn, 5.0);
        AnchorPane.setTopAnchor(topBtn, 20.0);
        AnchorPane.setLeftAnchor(leftBtn, 40.0);
        AnchorPane.setBottomAnchor(leftBtn, 40.0);

        primaryStage.setScene(new Scene(anchorPane, 400, 250));
        primaryStage.show();
    }

}

上面的例子中 leftBtn 设置了 Left 和 Bottom 两个停靠属性。AnchorPane.setxxxAnchor 方法的第二个参数用于设置边距。

 

这是在java中写的代码,在有fxml的的地方,直接在相关控件下添加即可

<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.control.TextArea?>
<GridPane fx:controller="sample.Controller"
          xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">
    <AnchorPane>
        <children>
            <TextArea fx:id="text" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0"
                      AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"/>
        </children>
    </AnchorPane>
</GridPane>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

离离原上草77

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值