例子
以下程序是磁贴窗格布局的示例。在此,我们正在创建一个包含 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.setTileMoognment(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 窗口,如下所示。