java中vbox是什么_VBox_学习JavaFx|WIKI教程

本文介绍了JavaFX中VBox布局的使用方法,包括其属性设置如对齐方式、子节点间距及边距等,并通过一个具体示例展示了如何创建一个包含文本框和按钮的VBox布局。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

VBox

如果我们在应用程序中使用VBox作为布局,则所有节点都设置在一个垂直列中。

包javafx.scene.layout名为VBox的类表示VBox窗格。 该类包含五个属性,它们是 -alignment - 此属性表示VBox边界内节点的对齐方式。 您可以使用setter方法setAlignment()为此属性设置值。

fillHeight - 此属性是布尔类型,并将此设置为true; VBox中可调整大小的节点的大小调整为VBox的高度。 您可以使用setter方法setFillHeight()为此属性设置值。

spacing - 此属性是double类型,它表示VBox的子节点之间的空间。 您可以使用setter方法setSpacing()为此属性设置值。

除此之外,本课程还提供以下方法 -setVgrow() - 设置VBox包含的子项的垂直增长优先级。 此方法接受节点和优先级值。

setMargin() - 使用此方法,可以将边距设置为VBox。 此方法接受Insets类的节点和对象(矩形区域的4个边的一组内部偏移)

例子 (Example)

以下程序是VBox布局的示例。 在这里,我们插入一个文本字段和两个按钮,播放和停止。 这是以10的间距完成的,每个边距都有尺寸 - (10,10,10,10)。

将此代码保存在名为VBoxExample.java的文件中。import javafx.application.Application;

import javafx.collections.ObservableList;

import javafx.geometry.Insets;

import javafx.scene.Scene;

import javafx.scene.control.Button;

import javafx.scene.control.TextField;

import javafx.stage.Stage;

import javafx.scene.layout.VBox;

public class VBoxExample extends Application {

@Override

public void start(Stage stage) {

//creating a text field

TextField textField = new TextField();

//Creating the play button

Button playButton = new Button("Play");

//Creating the stop button

Button stopButton = new Button("stop");

//Instantiating the VBox class

VBox vBox = new VBox();

//Setting the space between the nodes of a VBox pane

vBox.setSpacing(10);

//Setting the margin to the nodes

vBox.setMargin(textField, new Insets(20, 20, 20, 20));

vBox.setMargin(playButton, new Insets(20, 20, 20, 20));

vBox.setMargin(stopButton, new Insets(20, 20, 20, 20));

//retrieving the observable list of the VBox

ObservableList list = vBox.getChildren();

//Adding all the nodes to the observable list

list.addAll(textField, playButton, stopButton);

//Creating a scene object

Scene scene = new Scene(vBox);

//Setting title to the Stage

stage.setTitle("Vbox Example");

//Adding scene to the stage

stage.setScene(scene);

//Displaying the contents of the stage

stage.show();

}

public static void main(String args[]){

launch(args);

}

}

使用以下命令从命令提示符编译并执行保存的java文件。javac VBoxExample.java

java VBoxExample.java

执行时,上面的程序生成一个JavaFX窗口,如下所示。

4f8a094bf375a9a010898a997bbc1ad0.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值