JavaFX - TilePane 佈局



如果我們在應用程式中使用此面板,則新增到其中的所有節點都將以統一大小的磁貼形式排列。名為 tilePane 的類,位於 javafx.scene.layout 包中,表示 TilePane。

此類提供了 11 個屬性,它們是:

  • alignment - 此屬性表示面板的對齊方式,您可以使用 setAlignment() 方法設定此屬性的值。

  • hgap - 此屬性的型別為 double,它表示每行中每個磁貼之間的水平間距。

  • vgap - 此屬性的型別為 double,它表示每行中每個磁貼之間的垂直間距。

  • orientation - 此屬性表示每行中磁貼的方向。

  • prefColumns - 此屬性的型別為 double,它表示水平磁貼面板的首選列數。

  • prefRows - 此屬性的型別為 double,它表示垂直磁貼面板的首選行數。

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

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

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

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

  • tileAlignment - 此屬性的型別為 double,它表示每個子節點在其磁貼內的預設對齊方式。

示例

以下程式是磁貼面板佈局的示例。在此,我們正在建立一個包含 7 個按鈕的磁貼面板。

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

import javafx.application.Application; 
import javafx.collections.ObservableList; 
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 orientation for the Tile Pane 
      tilePane.setOrientation(Orientation.HORIZONTAL); 
       
      //Setting the alignment for the Tile Pane 
      tilePane.setTileAlignment(Pos.CENTER_LEFT); 
       
      //Setting the preferred columns for the Tile Pane 
      tilePane.setPrefRows(4);  
      
      //Retrieving the observable list of the Tile Pane 
      ObservableList list = tilePane.getChildren(); 
       
      //Adding the array of buttons to the pane 
      list.addAll(buttons);
	  
      //Creating a scene object 
      Scene scene = new Scene(tilePane);  
      
      //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 TilePaneExample.java 
java TilePaneExample

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

TilePane
javafx_layout_panes.htm
廣告

© . All rights reserved.