JavaFX布局-HBox

2024-05-12 02:20
文章标签 java 布局 fx hbox

本文主要是介绍JavaFX布局-HBox,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

JavaFX布局-HBox

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

  • HBox按照水平方向排列其子节点
  • 改变窗口大小,不会该部整体布局
  • 窗口太小会遮住内部元素,不会产生滚动条
    在这里插入图片描述

常用属性

alignment

对齐方式

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

spacing

子节点间的水平间距

new HBox().setSpacing(10);

children

子节点数据

new HBox().getChildren().add(null);
for (Node child : hBox.getChildren()) {// todo
}

margin

子节点边距

HBox.setMargin(hbox.getChildren().get(0), new Insets(5, 10, 5, 10));

padding

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

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

hgrow

设置额外的水平空间填充属性

HBox.setHgrow(hBox.getChildren().get(0), Priority.ALWAYS);

实现方式

Java实现

HBox hBox = new HBox();
// 设置子节点间的垂直间距
hBox.setSpacing(10);
// 设置子节点在hBox中对齐方式
hBox.setAlignment(Pos.CENTER_RIGHT);
// 容器边缘与其子节点之间的距离
hBox.setPadding(new Insets(10, 5, 10, 5));Label label = new Label("label1");
HBox.setMargin(label, new Insets(10, 5, 10, 5));
hBox.getChildren().add(label);for (int i = 0; i < 5; i++) {Button button = new Button();button.setId("btn_" + i);button.setText("按钮-" + i);HBox.setMargin(button, new Insets(10, 5, 10, 5));hBox.getChildren().add(button);
}// 最后一个节点填充剩余空间
HBox.setHgrow(hBox.getChildren().get(0), Priority.ALWAYS);

Xml实现

<HBox xmlns:fx="http://javafx.com/fxml" fx:controller="org.a.b.c.d.fx.demo.controller.HBoxController"alignment="CENTER_RIGHT" spacing="20"><Button text="Hello!"/><Button text="按钮1"/><Button text="按钮2"/><Button text="按钮3"/><Button text="按钮4"/><Button text="按钮5"/>
</HBox>

综合案例

在这里插入图片描述

BorderStroke borderStroke = new BorderStroke(Color.RED, BorderStrokeStyle.SOLID, new CornerRadii(5), new BorderWidths(1));
Border border = new Border(borderStroke);HBox hBox = new HBox();
// 设置子节点间的垂直间距
hBox.setSpacing(10);
// 设置子节点在hBox中对齐方式
hBox.setAlignment(Pos.CENTER_RIGHT);
// 容器边缘与其子节点之间的距离
hBox.setPadding(new Insets(10, 5, 10, 5));Label label = new Label("label1");
label.setBorder(border);
HBox.setMargin(label, new Insets(10, 5, 10, 5));
hBox.getChildren().add(label);for (int i = 0; i < 5; i++) {Pane pane = new Pane();pane.setId("pane_" + i);pane.setMinWidth(20);pane.setBorder(border);// 设置Pane边距HBox.setMargin(pane, new Insets(10, 5, 10, 5));hBox.getChildren().add(pane);
}// 第一个节点填充剩余空间
HBox.setHgrow(hBox.getChildren().get(0), Priority.ALWAYS);

这篇关于JavaFX布局-HBox的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/981335

相关文章

java对接第三方接口的三种实现方式

《java对接第三方接口的三种实现方式》:本文主要介绍java对接第三方接口的三种实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录HttpURLConnection调用方法CloseableHttpClient调用RestTemplate调用总结在日常工作

Spring 缓存在项目中的使用详解

《Spring缓存在项目中的使用详解》Spring缓存机制,Cache接口为缓存的组件规范定义,包扩缓存的各种操作(添加缓存、删除缓存、修改缓存等),本文给大家介绍Spring缓存在项目中的使用... 目录1.Spring 缓存机制介绍2.Spring 缓存用到的概念Ⅰ.两个接口Ⅱ.三个注解(方法层次)Ⅲ.

Spring Boot 整合 Redis 实现数据缓存案例详解

《SpringBoot整合Redis实现数据缓存案例详解》Springboot缓存,默认使用的是ConcurrentMap的方式来实现的,然而我们在项目中并不会这么使用,本文介绍SpringB... 目录1.添加 Maven 依赖2.配置Redis属性3.创建 redisCacheManager4.使用Sp

Spring Cache注解@Cacheable的九个属性详解

《SpringCache注解@Cacheable的九个属性详解》在@Cacheable注解的使用中,共有9个属性供我们来使用,这9个属性分别是:value、cacheNames、key、key... 目录1.value/cacheNames 属性2.key属性3.keyGeneratjavascriptor

redis在spring boot中异常退出的问题解决方案

《redis在springboot中异常退出的问题解决方案》:本文主要介绍redis在springboot中异常退出的问题解决方案,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴... 目录问题:解决 问题根源️ 解决方案1. 异步处理 + 提前ACK(关键步骤)2. 调整Redis消费者组

一文教你Java如何快速构建项目骨架

《一文教你Java如何快速构建项目骨架》在Java项目开发过程中,构建项目骨架是一项繁琐但又基础重要的工作,Java领域有许多代码生成工具可以帮助我们快速完成这一任务,下面就跟随小编一起来了解下... 目录一、代码生成工具概述常用 Java 代码生成工具简介代码生成工具的优势二、使用 MyBATis Gen

springboot项目redis缓存异常实战案例详解(提供解决方案)

《springboot项目redis缓存异常实战案例详解(提供解决方案)》redis基本上是高并发场景上会用到的一个高性能的key-value数据库,属于nosql类型,一般用作于缓存,一般是结合数据... 目录缓存异常实践案例缓存穿透问题缓存击穿问题(其中也解决了穿透问题)完整代码缓存异常实践案例Red

SpringCloud整合MQ实现消息总线服务方式

《SpringCloud整合MQ实现消息总线服务方式》:本文主要介绍SpringCloud整合MQ实现消息总线服务方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录一、背景介绍二、方案实践三、升级版总结一、背景介绍每当修改配置文件内容,如果需要客户端也同步更新,

java中XML的使用全过程

《java中XML的使用全过程》:本文主要介绍java中XML的使用全过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录什么是XML特点XML作用XML的编写语法基本语法特殊字符编写约束XML的书写格式DTD文档schema文档解析XML的方法​​DOM解析XM

Java 的 Condition 接口与等待通知机制详解

《Java的Condition接口与等待通知机制详解》在Java并发编程里,实现线程间的协作与同步是极为关键的任务,本文将深入探究Condition接口及其背后的等待通知机制,感兴趣的朋友一起看... 目录一、引言二、Condition 接口概述2.1 基本概念2.2 与 Object 类等待通知方法的区别