JavaFX布局-StackPane

  • 所有子节点堆叠在一起,通常最后一个添加的子节点会显示在最上面

常用属性

alignment

对齐方式

stackPane.setAlignment(Pos.CENTER_RIGHT);
public enum Pos {
   /**
    * Represents positioning on the top vertically and on the left horizontally.
    */
   TOP_LEFT(TOP, LEFT),

   /**
    * Represents positioning on the top vertically and on the center horizontally.
    */
   TOP_CENTER(TOP, HPos.CENTER),

   /**
    * Represents positioning on the top vertically and on the right horizontally.
    */
   TOP_RIGHT(TOP, RIGHT),

   /**
    * Represents positioning on the center vertically and on the left horizontally.
    */
   CENTER_LEFT(VPos.CENTER, LEFT),

   /**
    * Represents positioning on the center both vertically and horizontally.
    */
   CENTER(VPos.CENTER, HPos.CENTER),

   /**
    * Represents positioning on the center vertically and on the right horizontally.
    */
   CENTER_RIGHT(VPos.CENTER, RIGHT),

   /**
    * Represents positioning on the bottom vertically and on the left horizontally.
    */
   BOTTOM_LEFT(BOTTOM, LEFT),

   /**
    * Represents positioning on the bottom vertically and on the center horizontally.
    */
   BOTTOM_CENTER(BOTTOM, HPos.CENTER),

   /**
    * Represents positioning on the bottom vertically and on the right horizontally.
    */
   BOTTOM_RIGHT(BOTTOM, RIGHT),

   /**
    * Represents positioning on the baseline vertically and on the left horizontally.
    */
   BASELINE_LEFT(BASELINE, LEFT),

   /**
    * Represents positioning on the baseline vertically and on the center horizontally.
    */
   BASELINE_CENTER(BASELINE, HPos.CENTER),

   /**
    * Represents positioning on the baseline vertically and on the right horizontally.
    */
   BASELINE_RIGHT(BASELINE, RIGHT);
}    

padding

容器边缘与其子节点之间的距离

stackPane.setPadding(new Insets(5, 10, 5, 10));

实现方式

Java实现

在这里插入图片描述

public static StackPane demo1() {
        // 创建StackPane
        StackPane stackPane = new StackPane();

        // 对齐方式
        stackPane.setAlignment(Pos.CENTER);
        // 内边距
        stackPane.setPadding(new Insets(10, 10, 10, 10));

        // 圆形
        Circle circle = new Circle(100, Color.RED);
        // 矩形
        Rectangle rectangle = new Rectangle(120, 100, Color.BLUE);
        // 按钮
        Button button = new Button("Button 1");

        // 添加子节点
        stackPane.getChildren().addAll(circle, rectangle, button);

        return stackPane;
    }

fxml实现

<StackPane alignment="CENTER" prefHeight="400" prefWidth="600" xmlns="http://javafx.com/javafx/17.0.2-ea"
           xmlns:fx="http://javafx.com/fxml/1">
    <padding>
        <Insets bottom="10" left="10" right="10" top="10"/>
    </padding>
    <Circle fill="red" radius="100"/>
    <Rectangle fill="blue" height="100" width="120"/>
    <Button text="button 1"/>
</StackPane>
  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值