JavaFX - VBox 布局



JavaFX 中的 VBox 布局

VBox,也称为垂直盒,是一个布局控件,它将 JavaFX 应用程序的所有节点排列在一个垂直列中。VBox 布局面板由名为VBox 的类表示,该类属于javafx.scene.layout包。实例化此类将创建一个 VBox 布局。此类的构造函数列表如下:

  • VBox() - 这是默认构造函数,它创建一个间距为 0 的 VBox 布局。

  • VBox(double spacingVal) - 它创建一个新的 VBox 布局,节点之间的间距为指定值。

  • VBox(double spacingVal, Node nodes) - VBox 类的此参数化构造函数接受子节点以及它们之间的间距,并创建一个具有指定组件的新 VBox 布局。

  • VBox(Node nodes) - 它创建一个具有指定子节点且间距为 0 的 VBox 布局。

下图显示三个垂直布局的盒子:

VBox

VBox 类附带一些预定义的属性,如下所示:

  • alignment - 此属性表示 VBox 边界内节点的对齐方式。您可以使用设置方法setAlignment()来为此属性设置值。

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

  • spacing - 此属性为双精度类型,它表示 VBox 子节点之间的间距。您可以使用设置方法setSpacing()为此属性设置值。

  • padding - 它表示 VBox 的边框及其子节点之间的间距。我们可以使用设置方法setPadding()为此属性设置值,该方法接受Insets构造函数作为参数值。

除此之外,此类还提供以下方法:

序号 方法和描述
1 setVgrow()

设置子节点在 VBox 中包含时的垂直增长优先级。此方法接受一个节点和一个优先级值。

2 setMargin()

使用此方法,您可以为 VBox 设置边距。此方法接受一个节点和 Insets 类的一个对象(矩形区域四边的内部偏移量集合)。

示例

以下程序是VBox布局的示例。在此,我们插入一个文本字段和两个按钮,分别为播放和停止。将此代码保存在名为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 文件。

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

输出

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

VBox

示例

在此示例中,我们将使用 VBox 类的参数化构造函数来创建垂直布局。将此代码保存在名为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 文件。

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

输出

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

VBox
广告