JavaFX VBox

news2024/10/5 15:29:19

VBox布局将子节点堆叠在垂直列中。新添加的子节点被放置在上一个子节点的下面。默认情况下,VBox尊重子节点的首选宽度和高度。

当父节点不可调整大小时,例如Group节点,最大垂直列的宽度基于具有最大优选宽度的节点。
默认情况下,每个子节点与左上(Pos.TOP_LEFT)位置对齐。

示例

以下代码将TextArea控件设置为在调整父VBox的高度时垂直增长:

TextArea  myTextArea = new TextArea();
//设置后跟随外边框增长
VBox.setHgrow(myTextArea,  Priority.ALWAYS);

完整的代码如下所示

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        TextArea myTextArea = new TextArea();
        VBox hbox = new VBox();
        hbox.getChildren().add(myTextArea);
        //设置以后跟随外边框增长
        VBox.setVgrow(myTextArea, Priority.ALWAYS);
        
        Scene scene = new Scene(hbox, 320, 112, Color.rgb(0, 0, 0, 0));
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    
    public static void main(String[] args) {
        launch(args);
    }
}

示例

下面的代码使用四个矩形来演示VBox的使用。

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        Group root = new Group();
        Scene scene = new Scene(root, 300, 250);
        // 5 pixels space between child nodes
        VBox vbox = new VBox(5);
        // 1 pixel padding between child nodes only
        vbox.setPadding(new Insets(1));
        Rectangle r1 = new Rectangle(10, 10);
        Rectangle r2 = new Rectangle(20, 100);
        Rectangle r3 = new Rectangle(50, 20);
        Rectangle r4 = new Rectangle(20, 50);
        
        VBox.setMargin(r1, new Insets(2, 2, 2, 2));
        
        vbox.getChildren().addAll(r1, r2, r3, r4);
        root.getChildren().add(vbox);
        
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    
    public static void main(String[] args) {
        launch(args);
    }
}

VBox间距

VBox vbox = new VBox(8); // spacing = 8
vbox.getChildren().addAll(new Button("Cut"), new Button("Copy"), new Button("Paste"));

完整的实现代码如下所示

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application {
    
    @Override
    public void start(final Stage stage) {
        stage.setTitle("HTML");
        stage.setWidth(500);
        stage.setHeight(500);
        Scene scene = new Scene(new Group());
        
        VBox vbox = new VBox(8); // spacing = 8
        vbox.getChildren().addAll(new Button("Cut"), new Button("Copy"), new Button("Paste"));
        
        scene.setRoot(vbox);
        
        stage.setScene(scene);
        stage.show();
    }
    
    public static void main(String[] args) {
        launch(args);
    }
}

设置填充和间距

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;

public class Main extends Application {
    public static void main(String[] args) {
        Application.launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("VBox Test");
        
        // VBox
        VBox vb = new VBox();
        vb.setPadding(new Insets(10, 50, 50, 50));
        vb.setSpacing(10);
        
        Label lbl = new Label("VBox");
        lbl.setFont(Font.font("Amble CN", FontWeight.BOLD, 24));
        vb.getChildren().add(lbl);
        
        // Buttons
        Button btn1 = new Button();
        btn1.setText("Button1");
        vb.getChildren().add(btn1);
        
        Button btn2 = new Button();
        btn2.setText("Button2");
        vb.getChildren().add(btn2);
        
        Button btn3 = new Button();
        btn3.setText("Button3");
        vb.getChildren().add(btn3);
        
        Button btn4 = new Button();
        btn4.setText("Button4");
        vb.getChildren().add(btn4);
        
        // Adding VBox to the scene
        Scene scene = new Scene(vb);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

逢3必过报数游戏-第13届蓝桥杯省赛Python真题精选

[导读]:超平老师的Scratch蓝桥杯真题解读系列在推出之后,受到了广大老师和家长的好评,非常感谢各位的认可和厚爱。作为回馈,超平老师计划推出《Python蓝桥杯真题解析100讲》,这是解读系列的第84讲。 逢3必过报数游戏&…

guli商城业务逻辑-基础篇笔记

这里写目录标题 0.1 viscode设置用户代码片段1.实现多级菜单接口1.1 对接前端菜单1.2 对接网关接口解决跨域问题,如果不解决跨域,浏览器还是访问不了api1.3 把商品服务添加网关1.4 修改前端显示分类菜单1.5 给菜单添加删除修改功能1.5.1 删除功能的后端业…

安卓在Fragment控制状态栏显示隐藏

废话不多上效果 隐藏 显示 核心代码 首先是Framgrent package com.zx.tab;import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button;impor…

分布式系统中的经典思想实验——两将军问题和拜占庭将军问题

文章目录 一、两将军问题1.1 问题描述1.2 深入理解两将军问题1.3 实验结论 二、拜占庭将军问题2.1 问题描述2.2 深入理解拜占庭将军问题2.3 解决方案 三、两将军和拜占庭问题的关系3.1 区别和联系3.2 应用与现实意义 参考资料 一、两将军问题 1.1 问题描述 两将军问题描述的是…

使用了代理IP怎么还会被封?代理IP到底有没有效果

代理IP作为一种网络工具,被广泛应用于各种场景,例如网络爬虫、海外购物、规避地区限制等。然而,很多用户在使用代理IP的过程中却发现自己的账号被封禁,这让他们不禁产生疑问:使用了代理IP怎么还会被封?代理…

基于粒子群优化算法的的微电网多目标优化调度----算法改进

前言: 当阅读过前一篇我的博客之后,并且认真去读懂了那篇文章末尾的代码,那么,后续的算法改进对于你来说应当是很容易的了。前文中提及过,粒子群在进行迭代时,每迭代一次,都会根据自己个体最优值…

4 最简单的 C 程序设计—顺序程序设计-4.6 顺序结构程序设计举例

【例 4.14】输入三角形的三边长&#xff0c;求三角形面积。 #include <stdio.h> #include <math.h> // 包含数学函数头文件main() {/* 【例 4.14】输入三角形的三边长&#xff0c;求三角形面积。已知三角形的三边长 a,b,c&#xff0c;则该三角形的面积公式为&…

【Java04】引用变量数组初始化的内存机制

引用类型数组指向的元素也是引用。其本质是&#xff1a; 由一个在栈上的引用数组变量指向一块堆内存&#xff1b;这块堆内存里存储的元素是引用&#xff0c;又分别指向其他堆内存。 class Person // Person是一个自定义的类 {public int age;puiblic double height;public vo…

mybatis-plus使用拦截器实现sql完整打印

shigen坚持更新文章的博客写手&#xff0c;擅长Java、python、vue、shell等编程语言和各种应用程序、脚本的开发。记录成长&#xff0c;分享认知&#xff0c;留住感动。 个人IP&#xff1a;shigen 在使用mybatis-plus&#xff08;mybatis&#xff09;的时候&#xff0c;往往需要…

DDPM公式推导(四)

3 Diffusion models and denoising autoencoders 扩散模型可能看起来是一类受限制的潜在变量模型&#xff0c;但它们在实现中允许很大的自由度。必须选择正向过程的方差 β t \beta_t βt​以及逆向过程的模型架构和高斯分布参数化。为了指导我们的选择&#xff0c;我们在扩散…

解决必剪电脑版导出视频缺斤少两的办法

背景 前几天将电脑重置了&#xff0c;今天想要剪辑一下视频&#xff0c;于是下载了必剪&#xff0c;将视频、音频都调整好&#xff0c;导出&#xff0c;结果15分钟的视频只能导出很短的时长&#xff0c;调整参数最多也只能导出10分钟&#xff0c;My God&#xff01; 解决 首…

Win10“始终使用此应用打开”不见了怎么办?

问题背景 真是服了&#xff0c;昨天家里停电把我电脑系统盘固态烧掉了&#xff0c;于是换了个新的固态给电脑装上新系统。结果这个版本的Win10系统居然无法修改默认应用。具体问题见下面两个图&#xff0c;以py文件为例。 图一&#xff1a;“选择打开方式时没有始终使用此应用…

深入理解指针(四)

目录 1. 回调函数是什么? ​2. qsort使用举例 2.1冒泡排序 2.2使用qsort函数排序整型数据 ​2.3 使用qsort排序结构数据(名字) 2.4 使用qsort排序结构数据(年龄) 3. qsort函数的模拟实现 1. 回调函数是什么? 回调函数就是⼀个通过函数指针调⽤的函数。 如果你把函数…

计算机组成原理之定点加法与减法运算

文章目录 补码的加减法算法的流程与逻辑实现溢出判断溢出原因单符号位判断双符号位&#xff08;变形补码&#xff09; 基本的加法/减法器舍入方法习题 补码的加减法 数用补码表示&#xff0c;符号位参与运算 考虑几个问题&#xff1f; 1.实际操作能否只取决于操作码&#xff1f…

基于CentOS Stream 9平台 安装/卸载 Redis7.0.15

已更正systemctl管理Redis服务问题 1. 官方下载地址 https://redis.io/downloads/#redis-downloads 1.1 下载或上传到/opt/coisini目录下&#xff1a; mkdir /opt/coisini cd /opt/coisini wget https://download.redis.io/releases/redis-7.0.15.tar.gz2. 解压 tar -zxvf re…

docker拉取镜像太慢解决方案

前言 这是我在这个网站整理的笔记,有错误的地方请指出&#xff0c;关注我&#xff0c;接下来还会持续更新。 作者&#xff1a;神的孩子都在歌唱 创建daemon.json文件,输入以下信息 vim /etc/docker/daemon.json{"registry-mirrors": ["https://9cpn8tt6.mirror…

搭建WWW服务

1.实验环境的配置 【1】设置windows虚拟机server和test网络属性 打开虚拟机的【开始】菜单->【控制面板】->【网络连接】窗口。 1. 选中【本地连接】右击鼠标&#xff0c;选中【属性】&#xff0c;打开【本地连接属性】窗口。 2. 选择【网络】页签。 3. 在【此连接使…

vue3实现表格的分页以及确认消息弹窗

表格的分页实例展示 效果1:表格按照每行10条数据分页,且编号也会随之分页自增 实现按照页码分页效果 第二页 展示编号根据分页自动增长 固定表格高度 这边设置了滚动条,同时表格高度实现自适应滚动条高度 template部分 表格代码 编号是按照页码条数进行循环并根据索引自增…

理解DDD设计

DDD的理解 领域驱动设计&#xff08;Domain-Driven Design&#xff0c;DDD&#xff09;是一种软件开发方法论&#xff0c;强调将业务领域作为软件设计的核心&#xff0c;以便更好地满足业务需求。DDD认为&#xff0c;软件开发的核心是理解业务&#xff0c;而不是实现技术。在D…

Unity贪吃蛇改编【详细版】

Big and small greedy snakes 游戏概述 游戏亮点 通过对称的美感&#xff0c;设置两条贪吃蛇吧&#xff0c;其中一条加倍成长以及加倍减少&#xff0c;另一条正常成长以及减少&#xff0c;最终实现两条蛇对整个界面的霸占效果。 过程中不断记录两条蛇的得分情况&#xff0c…