Javafx 简明教程

JavaFX - VBox Layout

VBox Layout in JavaFX

VBox 也称为垂直盒子,它是一种布局控件,可将 JavaFX 应用程序的所有节点排列在单个垂直列中。VBox 布局窗格由名为 VBoxjavafx.scene.layout 包中的类表示。实例化此类会创建一个 VBox 布局。此类的构造函数列表如下 -

VBox which is also known as Vertical Box, is a layout control that arranges all the nodes of a JavaFX application in a single vertical column. The VBox layout pane is represented by a class named VBox of the package javafx.scene.layout. Instantiating this class will create an VBox layout. List of constructors of this class is as follows −

  1. VBox() − It is the default constructor that constructs an VBox layout with 0 spacing.

  2. VBox(double spacingVal) − It constructs a new VBox layout with the specified spacing between nodes.

  3. VBox(double spacingVal, Node nodes) − This parameterized constructor of VBox class accepts children nodes as well as spacing between them and creates a new VBox layout with the specified components.

  4. VBox(Node nodes) − It creates an VBox layout with specified children nodes and 0 spacing.

下图显示了三个以垂直布局方式放置的盒子 -

The figure below shows three boxes positioned in a vertical layout −

sample vbox

VBox 类具有几个预定义的属性,如下所示 -

The VBox class comes with several pre-defined properties which are listed below −

  1. alignment − This property represents the alignment of the nodes inside the bounds of the VBox. You can set value to this property by using the setter method setAlignment().

  2. fillHeight − This property is of Boolean type and on setting this to be true; the resizable nodes in the VBox are resized to the height of the VBox. You can set value to this property using the setter method setFillHeight().

  3. spacing − This property is of double type and it represents the space between the children of the VBox. You can set value to this property using the setter method setSpacing().

  4. padding − It represents the space between the border of VBox and its child nodes. We can set value to this property using the setter method setPadding() which accepts Insets constructor as a parameter value.

此外,此类还提供以下方法 -

In addition to these, this class also provides the following methods −

S.No

methods & Description

1

*setVgrow()*Sets the vertical grow priority for the child when contained by a VBox. This method accepts a node and a priority value.

2

*setMargin()*Using this method, you can set margins to the VBox. This method accepts a node and an object of the Insets class (A set of inside offsets for the 4 sides of a rectangular area).

Example

以下程序是 VBox 布局的示例。在此示例中,正在插入文本字段和两个按钮:播放和停止。将此代码保存在名为 VBoxExample.java 的文件中。

The following program is an example of the VBox layout. In this, we are inserting a text field and two buttons namely play and stop. Save this code in a file with the name VBoxExample.java.

import javafx.application.Application;
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 box = new VBox();

      //Setting the space between the nodes of a HBox pane
      box.setSpacing(10);

      //Adding all the nodes to the VBox
      box.getChildren().addAll(textField, playButton, stopButton);

      //Setting the margin to the nodes
      box.setMargin(textField, new Insets(20, 20, 20, 20));
      box.setMargin(playButton, new Insets(20, 20, 20, 20));
      box.setMargin(stopButton, new Insets(20, 20, 20, 20));

      //Creating a scene object
      Scene scene = new Scene(box, 400, 300);

      //Setting title to the Stage
      stage.setTitle("Vbox Example in JavaFX");

      //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 文件。

Compile and execute the saved java file from the command prompt using the following commands.

javac --module-path %PATH_TO_FX% --add-modules javafx.controls VBoxExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls VBoxExample

执行后,上述程序会生成如下所示的 JavaFX 窗口。

On executing, the above program generates a JavaFX window as shown below.

vbox output

Example

在此示例中,将使用 VBox 类的参数化构造函数创建一个垂直布局。将此代码保存在名为 VBoxExample.java 的文件中。

In this example, we will use the parameterized constructor of the VBox class to create a vertical layout. Save this code in a file with the name VBoxExample.java.

import javafx.application.Application;
import javafx.geometry.Pos;
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 box = new VBox(10, textField, playButton, stopButton);
      box.setAlignment(Pos.CENTER);

      //Creating a scene object
      Scene scene = new Scene(box, 400, 300);

      //Setting title to the Stage
      stage.setTitle("Vbox Example in JavaFX");

      //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 文件。

Compile and execute the saved java file from the command prompt using the following commands.

javac --module-path %PATH_TO_FX% --add-modules javafx.controls VBoxExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls VBoxExample

在执行上述程序时,将生成以下输出。

On executing, the above program generates the following output.

vbox output2