JavaFX - HBox 佈局



JavaFX 中的 HBox 佈局

HBox,也稱為水平盒,是一個佈局面板,它將 JavaFX 應用程式的所有節點排列在單個水平行中。HBox 佈局面板由名為HBox的類表示,該類屬於javafx.scene.layout包。例項化此類以建立 HBox 佈局。HBox 類的建構函式如下:

  • HBox() - 這是預設建構函式,它使用 0 間距構造一個 HBox 佈局。

  • HBox(double spacingVal) - 它使用節點之間指定的間距構造一個新的 HBox 佈局。

  • HBox(double spacingVal, Node nodes) - HBox 類的此引數化建構函式接受子節點以及它們之間的間距,並使用指定的元件建立一個新的 HBox 佈局。

  • HBox(Node nodes) - 它使用指定的子節點和 0 間距建立 HBox 佈局。

在下面的快照中,我們可以看到紅色框內的 UI 元件放置在水平佈局中:

HBox

HBox 類具有以下屬性:

  • alignment - 此屬性表示節點在 HBox 邊界內的對齊方式。我們可以使用 setter 方法setAlignment()為該屬性設定值。

  • fillHeight - 此屬性為布林型別,將其設定為 true 時,HBox 中的可調整大小的節點將調整為 HBox 的高度。我們可以使用 setter 方法setFillHeight()為該屬性設定值。

  • spacing - 此屬性為雙精度型別,它表示 HBox 子節點之間的空間。我們可以使用 setter 方法setSpacing()為該屬性設定值。

  • padding - 它表示 HBox 的邊框與其子節點之間的空間。我們可以使用 setter 方法setPadding()為該屬性設定值,該方法接受Insets建構函式作為引數值。

除了這些之外,HBox類還提供了一些方法,如下所示:

序號 方法和描述
1 setHgrow()

設定子節點在 HBox 中包含時的水平增長優先順序。此方法接受一個節點和一個優先順序值。可能的優先順序值可以是Policy.ALWAYSPolicy.SOMETIMESPolicy.NEVER

2 setMargin()

使用此方法,您可以為 HBox 設定邊距。此方法接受一個節點和一個 Insets 類物件(矩形區域 4 邊的內部偏移量集)。

示例

以下程式是 HBox 佈局的示例。在這裡,我們插入一個文字欄位和兩個名為播放和停止的按鈕。將此 JavaFX 程式碼儲存在名為HBoxExample.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.HBox;

public class HBoxExample 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 HBox class  
      HBox hbox = new HBox();    
      
      //Setting the space between the nodes of a HBox pane 
      hbox.setSpacing(10);    
      
      //Adding all the nodes to the HBox 
      hbox.getChildren().addAll(textField, playButton, stopButton);   

      //Setting the margin to the nodes 
      hbox.setMargin(textField, new Insets(20, 20, 20, 20)); 
      hbox.setMargin(playButton, new Insets(20, 20, 20, 20)); 
      hbox.setMargin(stopButton, new Insets(20, 20, 20, 20));      
      
      //Creating a scene object
      Scene scene = new Scene(hbox, 400, 300);  
      
      //Setting title to the Stage 
      stage.setTitle("Hbox 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 HBoxExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls HBoxExample

輸出

執行上述程式後,將生成一個 JavaFX 視窗,如下所示。

HBox

示例

在以下示例中,我們將使用 HBox 類的引數化建構函式來建立水平佈局。將此 JavaFX 程式碼儲存在名為HBoxExample.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.HBox;

public class HBoxExample 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 HBox class  
      HBox box = new HBox(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("Hbox 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 HBoxExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls HBoxExample

輸出

執行上述程式後,將生成以下輸出。

HBox
廣告

© . All rights reserved.