当前位置: 首页 > news >正文

JavaFX VBox

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 {@Overridepublic 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 {@Overridepublic void start(Stage primaryStage) {Group root = new Group();Scene scene = new Scene(root, 300, 250);// 5 pixels space between child nodesVBox vbox = new VBox(5);// 1 pixel padding between child nodes onlyvbox.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 {@Overridepublic 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 = 8vbox.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);}@Overridepublic void start(Stage primaryStage) {primaryStage.setTitle("VBox Test");// VBoxVBox 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);// ButtonsButton 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 sceneScene scene = new Scene(vb);primaryStage.setScene(scene);primaryStage.show();}
}

http://www.lryc.cn/news/374298.html

相关文章:

  • xss+csrf项目实例
  • 速盾:cdn加速怎么计费?
  • Vue3 中 props 与 emit 用法
  • 【python】如何import 另一个路径下的py文件内容
  • C/C++ Adaline自适应线性神经网络算法详解及源码
  • UniApp+Vue3使用Vant-微信小程序组件
  • python IP 端口 socket tcp 介绍
  • 【动态规划】| 路径问题之最小路径和 力扣64
  • 如何在vector中插入和删除元素?
  • 独具韵味的移动端 UI 风格
  • 【SpringBoot】SpringBoot:构建实时聊天应用
  • 基于Matlab的车牌识别停车场出入库计时计费管理系统(含GUI界面)【W6】
  • 大众点评js逆向过程(未完)
  • web前端如何设置单元格:深入解析与实用技巧
  • 龙迅LT9611UXC 2 PORT MIPIDSI/CSI转HDMI 2.1,支持音频IIS/SPDIF输入,支持标准4K60HZ输出
  • 红黑树(C++)
  • PyCharm设置不默认打开上次的项目
  • Eureka到Nacos迁移实战:解决配置冲突与启动异常
  • k8s 小技巧: 查看 Pod 上运行的容器
  • 【Git】基础操作
  • Linux:基础IO(二.缓冲区、模拟一下缓冲区、详细讲解文件系统)
  • 事件传播机制 与 责任链模式
  • uniapp 展示地图,并获取当前位置信息(精确位置)
  • Autosar实践——诊断配置(DaVinci Configuration)
  • 植物大战僵尸杂交版全新版v2.1解决全屏问题
  • 【code-server】Code-Server 安装部署
  • 博客摘录「 YOLOv5模型剪枝压缩」2024年5月11日
  • HttpSecurity
  • Mysql union语句
  • MySQL之高级特性(四)