[英]How to attach Event Handler to JavaFX Stage/Window Minimize Button?
[英]How to access the event handler of javafx dynamic button
如何在網格窗格中獲取動態按鈕的事件處理程序?
Stage window;
public void start(Stage primaryStart) throws Exception{
window = primaryStart;
window.setTitle("Minesweeper (Eventually)");
//GridPane with 10px padding around edge
GridPane grid = new GridPane();
grid.setPadding(new Insets(10, 10, 10, 10));
for(int i = 0; i < 5; i++){
for(int j = 0; j<5; j++){
Button button = new Button("[]");
GridPane.setConstraints(button, i,j);
grid.getChildren().add(button);
}
}
Scene scene = new Scene(grid, 300, 200);
window.setScene(scene);
window.show();
button.setOnAction(e -> {System.out.println("Something");});
}
因為setOnAction在引用按鈕對象時給我一個錯誤。
您只需要用此替換代碼。
public void start(Stage primaryStart) throws Exception{
window = primaryStart;
window.setTitle("Minesweeper (Eventually)");
//GridPane with 10px padding around edge
GridPane grid = new GridPane();
grid.setPadding(new Insets(10, 10, 10, 10));
for(int i = 0; i < 5; i++){
for(int j = 0; j<5; j++){
Button button = new Button("[]");
button.setOnAction(e -> {System.out.println("Something");});
GridPane.setConstraints(button, i,j);
grid.getChildren().add(button);
}
}
Scene scene = new Scene(grid, 300, 200);
window.setScene(scene);
window.show();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.