JavaFX布局-FlowPane

  • 以水平或垂直的方式排列其子节点
  • 当空间不足的时候,自动换行以继续排列剩余的节点

常用属性

Alignment

对齐方式,设置内容居中,顶部居中等

flowPane.setAlignment(Pos.TOP_CENTER);
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);
}  

orientation

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

flowPane.setOrientation(Orientation.HORIZONTAL);

hgap

水平间距

flowPane.setHgap(10);

vgap

垂直间距

flowPane.setVgap(10);

padding

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

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

实现方式

Java实现

在这里插入图片描述

    public static FlowPane demo1() {
        // 创建FlowPane
        FlowPane flowPane = new FlowPane();

        // 对齐方式
        flowPane.setAlignment(Pos.TOP_CENTER);
        // 内边距
        flowPane.setPadding(new Insets(10, 10, 10, 10));
        // 水品排列
        flowPane.setOrientation(Orientation.HORIZONTAL);
        // 水平间距
        flowPane.setHgap(10);
        // 垂直间距
        flowPane.setVgap(10);
        // 首选换行长度
        flowPane.setPrefWrapLength(400);

        // 创建按钮
        for (int i = 0; i < 10; i++) {
            Button btn = new Button("Button " + (i + 1));
            // 将按钮添加到FlowPane
            flowPane.getChildren().addAll(btn);
        }

        return flowPane;
    }

fxml实现

<FlowPane xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1" orientation="VERTICAL"
          alignment="TOP_LEFT" prefWidth="600" prefHeight="400" vgap="10" hgap="10">
    <padding>
        <Insets left="10" top="5" right="10" bottom="5"/>
    </padding>
    <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"/>
</FlowPane>
  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值