JavaFX - FlowPane



如果我們在應用程式中使用 FlowPane,則所有節點都將包裝在一個流中。水平 FlowPane 在其高度處包裝窗格的元素,而垂直 FlowPane 在其寬度處包裝元素。

名為 FlowPane 的類,位於 javafx.scene.layout 包中,表示 FlowPane。此類包含 7 個屬性,包括:

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

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

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

  • Hgap - 此屬性為雙精度型別,表示 FlowPane 的行/列之間的水平間隙。

  • Orientation - 此屬性表示 FlowPane 的方向。

  • Vgap - 此屬性為雙精度型別,表示 FlowPane 的行/列之間的垂直間隙。

示例

以下程式是 FlowPane 佈局的示例。在這裡,我們在水平 FlowPane 中插入四個按鈕。

將此程式碼儲存在名為 FlowPaneExample.java 的檔案中。

import javafx.collections.ObservableList; 
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)); 
       
      //Retrieving the observable list of the flow Pane 
      ObservableList list = flowPane.getChildren(); 
      
      //Adding all the nodes to the flow pane 
      list.addAll(button1, button2, button3, button4); 
        
      //Creating a scene object 
      Scene scene = new Scene(flowPane);  
      
      //Setting title to the Stage 
      stage.setTitle("Flow Pane Example"); 
         
      //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 FlowPaneExample.java 
java FlowPaneExample

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

FlowPane
javafx_layout_panes.htm
廣告

© . All rights reserved.