多读书多实践,勤思考善领悟

JavaFX Layout TilePane(布局平铺面板)

本文于1660天之前发表,文中内容可能已经过时。

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

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

  • alignment - 此属性表示窗格的对齐方式,您可以使用setAlignment()方法设置此属性的值。
  • hgap - 此属性的类型为double,它表示行中每个tile之间的水平间距。
  • vgap - 此属性的类型为double,它表示行中每个tile之间的垂直间距。
  • orientation - 此属性表示行中切片的方向。
  • prefColumns - 此属性为double类型,它表示水平tile窗格的首选列数。
  • prefRows - 此属性是double类型,它表示垂直tile窗格的首选行数。
  • prefTileHeight - 此属性是double类型,它表示每个tile的首选高度。
  • prefTileWidth - 此属性为double类型,表示每个tile的首选宽度。
  • tileHeight - 此属性是double类型,它表示每个tile的实际高度。
  • tileWidth - 此属性为double类型,表示每个tile的实际宽度。
  • tileAlignment - 此属性是double类型,它表示其tile中每个子项的默认对齐方式。

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

将此代码保存在名为TilePaneExample.java的文件中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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文件。

1
2
javac TilePaneExample.java 
java TilePaneExample

执行时,上面的程序生成一个JavaFX窗口,如下所示。

TilePane