JavaFX布局-TilePane

  • 自动排列其子节点成网格状
  • 根据容器的大小以及子节点的数量和大小自动计算最佳的排列方式
  • 推荐子节点固定大小,参差不齐的子节点,效果很诡异

常用属性

alignment

对齐方式

tilePane.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);
}    

tileAlignment

子节点对齐方式

tilePane.setTileAlignment(Pos.CENTER);

orientation

排列方式,Orientation.VERTICAL、Orientation.HORIZONTAL

tilePane.setOrientation(Orientation.HORIZONTAL);

hgap

水平间距

tilePane.setHgap(10);

vgap

垂直间距

tilePane.setVgap(10);

padding

内边距,可以单独设置上、下、左、右的内边距

tilePane.setPadding(new Insets(10, 10, 10, 10));

实现方式

Java

在这里插入图片描述

    public static TilePane demo1() {
        // 创建TilePane
        TilePane tilePane = new TilePane();
        // 容器位置
        tilePane.setAlignment(Pos.BOTTOM_LEFT);
        // 子节点位置
        tilePane.setTileAlignment(Pos.CENTER);
        // 排列方向
        tilePane.setOrientation(Orientation.HORIZONTAL);
        // 水平间距
        tilePane.setHgap(10);
        // 垂直间距
        tilePane.setVgap(10);
        // 内边距
        tilePane.setPadding(new Insets(10, 10, 10, 10));

        // 圆形
        Circle circle = new Circle(50, Color.RED);
        tilePane.getChildren().addAll(circle);

        // 矩形
        Rectangle rectangle = new Rectangle(100, 80, Color.BLUE);
        tilePane.getChildren().addAll(rectangle);

        // 按钮
        for (int i = 0; i < 10; i++) {
            Button button = new Button("Button " + (i + 1));
            tilePane.getChildren().addAll(button);
        }

        return tilePane;
    }

fxml

<TilePane xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1" prefHeight="400"
          prefWidth="600" alignment="BOTTOM_LEFT" tileAlignment="CENTER" vgap="10" hgap="10" orientation="HORIZONTAL">
    <padding>
        <Insets left="10" top="5" right="10" bottom="5"/>
    </padding>
    <children>
        <Circle fill="red" radius="50"/>
        <Rectangle fill="blue" height="80" width="100"/>
        <Button text="button 1"/>
        <Button text="button 2"/>
        <Button text="button 3"/>
        <Button text="button 4"/>
        <Button text="button 5"/>
        <Button text="button 6"/>
        <Button text="button 7"/>
        <Button text="button 8"/>
        <Button text="button 9"/>
        <Button text="button 10"/>
    </children>
</TilePane>

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
StackPaneJavaFX 中常用的布局之一,它可以将多个控件放置在同一位置,通过设置控件的堆叠顺序来显示所需的控件。在 StackPane 中,所有控件默认都是居中对齐的,而且它们会自动适应窗口大小的变化。 下面是一个简单的 StackPane 示例代码: ```java import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.StackPane; import javafx.stage.Stage; public class StackPaneExample extends Application { @Override public void start(Stage primaryStage) { Button button1 = new Button("Button 1"); Button button2 = new Button("Button 2"); Button button3 = new Button("Button 3"); StackPane stackPane = new StackPane(); stackPane.getChildren().addAll(button1, button2, button3); Scene scene = new Scene(stackPane, 300, 200); primaryStage.setTitle("StackPane Example"); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } } ``` 在这个示例代码中,我们创建了三个按钮,并将它们添加到 StackPane 中。最后,我们创建了一个 Scene 并将 StackPane 设置为根节点,然后将该 Scene 设置为 primaryStage 的场景。运行程序,你可以看到三个按钮都被居中显示在窗口中央。 当然,StackPane 不仅仅只能用于放置按钮,你可以将任何控件添加到 StackPane 中,例如图片、标签、文本框等等。在 StackPane 中,后添加的控件会自动放在前面添加的控件上面,你可以通过设置控件的 Z 轴坐标来改变它们的堆叠顺序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值