JavaFX布局-VBox

JavaFX布局-VBox

  • 常用属性
    • alignment
    • spacing
    • children
    • margin
    • padding
    • vgrow
  • 实现方式
    • Java实现
    • Xml实现
  • 综合案例

  • VBox按照垂直顺序从上到下排列其子节点
  • 改变窗口大小,不会该部整体布局
  • 窗口太小会遮住内部元素,不会产生滚动条
    在这里插入图片描述

常用属性

alignment

对齐方式

参考枚举值:

new VBox().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);

spacing

定义子节点之间的垂直间距

new VBox().setSpacing(20);

children

包含所有子节点的列表,通常使用 getChildren().add(node) 方法添加元素

new VBox().getChildren().add(new Label("label1"));

margin

子节点边距

VBox.setMargin(vbox.getChildren().get(0), new Insets(10, 20, 30, 40));

padding

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

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

vgrow

设置额外的垂直空间填充属性,子节点可以用Pane来体现

VBox.setVgrow(vbox.getChildren().get(0), Priority.ALWAYS);

实现方式

Java实现

 VBox vbox = new VBox();// 设置子节点间的垂直间距vbox.setSpacing(20);// 设置子节点在VBox中对齐方式vbox.setAlignment(Pos.TOP_CENTER);vbox.getChildren().add(new Label("label1"));for (int i = 0; i < 5; i++) {vbox.getChildren().add(new Button("Button " + (i + 1)));}Scene scene = new Scene(vbox, 400, 300);

Xml实现

<VBox xmlns:fx="http://javafx.com/fxml" fx:controller="org.a.b.c.d.fx.demo.HelloController" alignment="BASELINE_CENTER"spacing="20"><Label fx:id="welcomeText"/><Button text="Hello!" onAction="#onHelloButtonClick"/><Button text="按钮1"/><Button text="按钮2"/><Button text="按钮3"/><Button text="按钮4"/><Button text="按钮5"/>
</VBox>

综合案例

在这里插入图片描述

// 边框线
BorderStroke borderStroke = new BorderStroke(Color.RED, BorderStrokeStyle.SOLID, new CornerRadii(5), new BorderWidths(1));
Border border = new Border(borderStroke);VBox vbox = new VBox();
// 设置子节点间的垂直间距
vbox.setSpacing(10);
// 设置子节点在VBox中对齐方式
vbox.setAlignment(Pos.TOP_CENTER);Label label = new Label("label1");
label.setBorder(border);
VBox.setMargin(label, new Insets(10, 40, 10, 40));
vbox.getChildren().add(label);for (int i = 0; i < 5; i++) {Pane pane = new Pane();pane.setId("pane_" + i);pane.setMinHeight(20);pane.setBorder(border);// 设置Pane边距VBox.setMargin(pane, new Insets(5, 20, 5, 20));vbox.getChildren().add(pane);
}// 设置margin
int num = vbox.getChildren().size();
// 最后一个节点填充剩余空间
VBox.setVgrow(vbox.getChildren().get(num - 1), Priority.ALWAYS);

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/690115.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

渗透思考题

一&#xff0c;尝试登录。 客户端对密码进行哈希处理并缓存密码hash&#xff0c;丢弃实际的明文密码&#xff0c;然后将用户名发送到服务器&#xff0c;发起认证请求 密文存储位置&#xff1a;数据库文件位于C:WindowsSystem32configsam&#xff0c;同时挂载在注册表中的HKLMSA…

Apache Sqoop:高效数据传输工具搭建与使用教程

目录 引言一、环境准备二、安装sqoop下载sqoop包解压文件 三、配置Sqoop下载mysql驱动拷贝hive的归档文件配置环境变量修改sqoop-env.sh配置文件替换版本的commons-lang的jar包 验证Sqoop安装查看Sqoop版本测试Sqoop连接MySQL数据库是否成功查看数据库查看数据表去除警告信息 四…

暗区突围PC测试资格获取教程 人人可领100%获取方法

暗区突围PC测试资格获取教程 人人可领100%获取方法 暗区突围国际服的上线&#xff0c;近日在游戏圈内掀起了不小的浪花。而一个暗区突围国际服的测试资格更是成为了玩家们眼中炙手可热的宝物。许多玩家不知道该如何获取游戏的测试资格&#xff0c;今天小编就为大家带来详细的教…

[数据集][目标检测]纸箱子检测数据集VOC+YOLO格式8375张1类别

数据集格式&#xff1a;Pascal VOC格式YOLO格式(不包含分割路径的txt文件&#xff0c;仅仅包含jpg图片以及对应的VOC格式xml文件和yolo格式txt文件) 图片数量(jpg文件个数)&#xff1a;8375 标注数量(xml文件个数)&#xff1a;8375 标注数量(txt文件个数)&#xff1a;8375 标注…

Python专题:八、列表(1)

Python的内置数据类型 数据类型&#xff1a;列表 list类型 可以是字符串&#xff0c;浮点数&#xff0c;整数&#xff0c;列表 列表特性 ①集合性的数据类型 ②列表是有序的 ③列表是可更新的 访问列表元素的方式也是[索引]&#xff0c;也是从0开始的&#xff0c;不能超过…

付费文章合集第二期

☞☞付费文章合集第一期 感谢大家一年来的陪伴与支持&#xff01; 对于感兴趣的文章点标题能跳转原文阅读啦~~ 21、Matlab信号处理——基于LSB和DCB音频水印嵌入提取算法 22、CV小目标识别——AITOD数据集&#xff08;已处理&#xff09; 23、Matlab信号发生器——三角波、…

报表-设计器的使用

1、设计器目录结构 报表设计器以压缩包的方式提供&#xff0c;解压后&#xff0c;目录结构如下&#xff1a; 目录说明&#xff1a; 1、jdk-17&#xff1a;压缩包中自带的windows平台下的jdk17 2、lite-report&#xff1a;报表文件和数据源配置文件的保存位置 3、lite-repor…

ARM基于DWT实现硬件延时(GD32)

软件延时的缺点 软件延时的精度差&#xff0c;受系统主频影响&#xff0c;调教困难 硬件延时 DWT数据跟踪监视点单元硬件延时 硬件延时实现代码 delay.c #include <stdint.h> #include "gd32f30x.h"/** *****************************************************…

[GESP样题 四级] 填幻方和幸运数

B3940 [GESP样题 四级] 填幻方 题目 在一个NN 的正方形网格中&#xff0c;每个格子分别填上从 1 到 NN 的正整数&#xff0c;使得正方形中任一行、任一列及对角线的几个数之和都相等&#xff0c;则这种正方形图案就称为“幻方”&#xff08;输出样例中展示了一个33 的幻方&am…

营销的本质是“利他”,资深运营高手分享9套消费返利玩转市场!

营销的本质是“利他”&#xff0c;资深运营高手分享9套消费返利玩转市场&#xff01; 文丨微三云营销总监胡佳东&#xff0c;点击上方“关注”&#xff0c;为你分享市场商业模式电商干货。 - 引言&#xff1a;2024年移动互联网基本已经占据了核心不可篡改的地位&#xff0c;而…

git-删除workspace.xml的跟踪

问题描述 .gitignore 文件内容如下&#xff1a; .pyc *.pyc user_files/ .vscode/ __pycache__//.idea/misc.xml /.idea/modules.xml /.idea/inspectionProfiles/profiles_settings.xml /.idea/inspectionProfiles/Project_Default.xml /.idea/batrp_webbackend-server-dev.i…

C语言实战项目---通讯录

项目要实现的内容&#xff1a;能够存放100个人的通讯录程序&#xff0c;能够实现联系人数据的存储&#xff0c;删除&#xff0c;修改&#xff0c;查找&#xff0c;展示联系人的信息。 所需知识&#xff1a;结构体&#xff0c;指针&#xff0c;函数................. 废话不多…