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
廣告