JavaFX - FlowPane 佈局



JavaFX 中的 FlowPane 佈局

FlowPane 佈局將其所有節點排列成流。在水平 FlowPane 中,元素根據其高度進行換行,而在垂直 FlowPane 中,元素根據其寬度進行換行。

在 JavaFX 中,名為 FlowPane 的類(位於 javafx.scene.layout 包中)表示 FlowPane。要在我們的 JavaFX 應用程式中建立 FlowPane 佈局,請使用以下任何建構函式例項化此類:

  • FlowPane() - 它構造一個新的水平 FlowPane 佈局。

  • FlowPane(double hGap, double vGap) - 它建立一個新的水平 FlowPane 佈局,並具有指定的 hGap 和 vGap。

  • FlowPane(double hGap, double vGap, Node childNodes) - 構造一個水平 FlowPane 佈局,並具有指定的 hGap、vGap 和節點。

  • FlowPane(Orientation orientation) - 它建立一個新的 FlowPane 佈局,並具有指定的 orientation。它可以是 HORIZONTAL 或 VERTICAL。

此類包含以下屬性:

序號 屬性和描述
1 alignment

此屬性表示 FlowPane 內容的對齊方式。我們可以使用 setter 方法 setAllignment() 設定此屬性。

2 columnHalignment

此屬性表示垂直 FlowPane 中節點的水平對齊方式。

3 rowValignment

此屬性表示水平 FlowPane 中節點的垂直對齊方式。

4 Hgap

此屬性為 double 型別,它表示 FlowPane 的行/列之間的水平間距。

5 Orientation

此屬性表示 FlowPane 的方向。

6 Vgap

此屬性為 double 型別,它表示 FlowPane 的行/列之間的垂直間距。

示例

以下程式是 FlowPane 佈局的一個示例。在此,我們將四個按鈕插入水平 FlowPane 中。將此程式碼儲存在名為 FlowPaneExample.java 的檔案中。

import javafx.application.Application; 
import javafx.geometry.Insets; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.layout.FlowPane; 
import javafx.scene.shape.Sphere; 
import javafx.stage.Stage; 
         
public class FlowPaneExample extends Application { 
   @Override 
   public void start(Stage stage) {      
      //Creating button1 
      Button button1 = new Button("Button1");       
      
      //Creating button2 
      Button button2 = new Button("Button2");       
      
      //Creating button3
      Button button3 = new Button("Button3");       
      
      //Creating button4 
      Button button4 = new Button("Button4");       
      
      //Creating a Flow Pane 
      FlowPane flowPane = new FlowPane();    
       
      //Setting the horizontal gap between the nodes 
      flowPane.setHgap(25); 
       
      //Setting the margin of the pane  
      flowPane.setMargin(button1, new Insets(20, 0, 20, 20)); 
      
      //Adding all the nodes to the flow pane 
      flowPane.getChildren().addAll(button1, button2, button3, button4); 
        
      //Creating a scene object 
      Scene scene = new Scene(flowPane, 400, 300);  
      
      //Setting title to the Stage 
      stage.setTitle("Flow Pane 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 FlowPaneExample.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls FlowPaneExample

輸出

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

FlowPane

示例

在以下示例中,我們將建立一個方向為垂直的 FlowPane。要設定方向,我們將使用 FlowPane 類的帶引數建構函式,並將 Orientation.VERTICAL 列舉值作為引數傳遞。將此程式碼儲存在名為 JavafxFlowpane.java 的檔案中。

import javafx.application.Application; 
import javafx.geometry.Orientation; 
import javafx.geometry.Pos; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.layout.FlowPane; 
import javafx.scene.shape.Sphere; 
import javafx.stage.Stage; 
         
public class JavafxFlowpane extends Application { 
   @Override 
   public void start(Stage stage) {      
      //Creating button1 
      Button button1 = new Button("Button1");       
      
      //Creating button2 
      Button button2 = new Button("Button2");       
      
      //Creating button3
      Button button3 = new Button("Button3");       
      
      //Creating button4 
      Button button4 = new Button("Button4");       
      
      //Creating a Flow Pane 
      FlowPane flowPane = new FlowPane(Orientation.VERTICAL);    
       
      //Setting the horizontal gap between the nodes 
      flowPane.setVgap(15); 
       
      //Setting the margin of the pane  
      flowPane.setAlignment(Pos.CENTER); 
      
      //Adding all the nodes to the flow pane 
      flowPane.getChildren().addAll(button1, button2, button3, button4); 
        
      //Creating a scene object 
      Scene scene = new Scene(flowPane, 400, 300);  
      
      //Setting title to the Stage 
      stage.setTitle("Flow Pane 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 JavafxFlowpane.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxFlowpane

輸出

當我們執行上述 Java 程式時,它將生成一個 JavaFX 視窗,顯示以下輸出:

FlowPane
廣告