目录

TilePane

如果我们在应用程序中使用此窗格,则添加到其中的所有节点将以统一大小的切片的形式排列。 包javafx.scene.layout名为tilePane的类表示TilePane。

这个类提供了11个属性,它们是 -

  • alignment - 此属性表示窗格的对齐方式,您可以使用setAlignment()方法设置此属性的值。

  • hgap - 此属性的类型为double,它表示行中每个tile之间的水平间距。

  • vgap - 此属性的类型为double,它表示行中每个tile之间的垂直间距。

  • orientation - 此属性表示行中切片的方向。

  • prefColumns - 此属性为double类型,它表示水平切片窗格的首选列数。

  • prefRows - 此属性是double类型,它表示垂直tile窗格的首选行数。

  • prefTileHeight - 此属性是double类型,它表示每个tile的首选高度。

  • prefTileWidth - 此属性为double类型,表示每个tile的首选宽度。

  • tileHeight - 此属性是double类型,它表示每个tile的实际高度。

  • tileWidth - 此属性是double类型,它表示每个tile的实际宽度。

  • tileAlignment - 此属性是double类型,它表示其tile中每个子项的默认对齐方式。

例子 (Example)

以下程序是平铺窗格布局的示例。 在此,我们正在创建一个包含7个按钮的平铺窗格。

将此代码保存在名为TilePaneExample.java.的文件中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
↑回到顶部↑
WIKI教程 @2018