使用迴圈動態生成頁面按鈕
Sample.fxml
<?xml version="1.0" encoding="UTF-8"?>
<Pane prefHeight="600.0" prefWidth="1000.0" xmlns="http://javafx.com/javafx/8.0.221" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.var.client.netty.Controller">
<children>
<ScrollPane layoutY="100.0" prefHeight="500.0" prefWidth="220.0">
<content>
<GridPane fx:id="protoButtonGrid" hgap="10.0" maxHeight="Infinity" maxWidth="Infinity" vgap="5">
</GridPane>
</content>
</ScrollPane>
</children>
</Pane>
Controller.java
package com.var.client.netty;
public class Controller {
@FXML
GridPane buttonGrid;
public void initialize(){
for (int i=0; i<5;i++){
addProtoButton("key", "name", i);
}
}
private void addProtoButton(Integer key, String simpleName, int order) {
String name = key + ":" + simpleName;
Button button = new Button(name);
button.setStyle("-fx-text-alignment: left; -fx-background-color: #ca3e47;-fx-text-fill: #ffffff");
button.setPrefSize(187, 40);
button.setId("button_" + order + 1);
button.setOnAction(event -> {
//TODO 按鈕事件
});
buttonGrid.add(button , 1, order + 1);
}
}