JavaFX - TilePane 佈局



JavaFX 中的 TilePane 佈局

在 JavaFX 中,TilePane 是一種佈局元件,它以統一大小的磁貼形式排列其子節點,可以水平或垂直排列。我們可以控制行數或列數、磁貼之間的間隙、窗格的對齊方式以及每個磁貼的首選大小。名為 TilePane 的類屬於 javafx.scene.layout 包,表示 TilePane。要建立 TilePane,我們可以使用以下任何建構函式:

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

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

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

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

此類提供了 11 個屬性,如下所示:

序號 屬性及描述
1 alignment

此屬性表示窗格的對齊方式,其值可以透過使用 setAlignment() 方法設定。

2 hgap

此屬性為 double 型別,表示一行中每個磁貼之間的水平間隙。

3 vgap

此屬性為 double 型別,表示一行中每個磁貼之間的垂直間隙。

4 orientation

此屬性表示一行中磁貼的方向。

5 prefColumns

此屬性為 double 型別,表示水平磁貼窗格的首選列數。

6 prefRows

此屬性為 double 型別,表示垂直磁貼窗格的首選行數。

7 prefTileHeight

此屬性為 double 型別,表示每個磁貼的首選高度。

8 prefTileWidth

此屬性為 double 型別,表示每個磁貼的首選寬度。

9 tileHeight

此屬性為 double 型別,表示每個磁貼的實際高度。

10 tileWidth

此屬性為 double 型別,表示每個磁貼的實際寬度。

11 tileAlignment

此屬性為 double 型別,表示每個子元素在其磁貼內的預設對齊方式。

示例

以下程式是磁貼窗格佈局的示例。在此,我們建立一個包含 7 個按鈕的磁貼窗格。預設情況下,其方向為水平。將此程式碼儲存在名為 TilePaneExample.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.TilePane; 
import javafx.stage.Stage;

public class TilePaneExample extends Application { 
   @Override 
   public void start(Stage stage) {    
      //Creating an array of Buttons 
      Button[] buttons = new Button[] { 
         new Button("SunDay"), 
         new Button("MonDay"), 
         new Button("TuesDay"), 
         new Button("WednesDay"), 
         new Button("ThursDay"), 
         new Button("FriDay"), 
         new Button("SaturDay")  
      };   
      //Creating a Tile Pane 
      TilePane tilePane = new TilePane();   
       
      //Setting the alignment for the Tile Pane 
      tilePane.setTileAlignment(Pos.CENTER_LEFT); 
       
      //Setting the preferred columns for the Tile Pane 
      tilePane.setPrefRows(4);  
      
      //Adding the array of buttons to the pane 
      tilePane.getChildren().addAll(buttons);
	  
      //Creating a scene object 
      Scene scene = new Scene(tilePane, 400, 300);  
      
      //Setting title to the Stage 
      stage.setTitle("Tile 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 --module-path %PATH_TO_FX% --add-modules javafx.controls TilePaneExample.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls TilePaneExample

輸出

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

TilePane

將 TilePane 的方向設定為垂直

要在 JavaFX 中設定 TilePane 的方向,我們使用名為 setOrientation() 的內建方法,或者使用其接受方向作為引數值的帶引數建構函式。以下 JavaFX 程式碼說明了如何將 TilePane 的方向設定為垂直。將此 JavaFX 程式碼儲存在名為 JavafxTilepane 的檔案中。

示例

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.TilePane;
import javafx.stage.Stage;

public class JavafxTilepane extends Application {
   @Override
   public void start(Stage stage) {
      // Creating a TilePane with vertical orientation
      TilePane tileP = new TilePane(Orientation.VERTICAL);
      // Setting the preferred number of rows to three
      tileP.setPrefRows(3);

      // Setting the hGap and vGap between tiles
      tileP.setHgap(10);
      tileP.setVgap(10);
      // Setting the alignment of the pane and the tiles
      tileP.setAlignment(Pos.CENTER);
      tileP.setTileAlignment(Pos.CENTER);
      // To add 10 buttons to the pane
      for (int i = 1; i <= 10; i++) {
         Button button = new Button("Button " + i);
         tileP.getChildren().add(button);
      }
      // Create a scene and stage
      Scene scene = new Scene(tileP, 400, 300);
      stage.setTitle("TilePane in JavaFX");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String[] args) {
      launch(args);
   }
}

要從命令提示符編譯並執行儲存的 Java 檔案,請使用以下命令:

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

輸出

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

TilePane output
廣告