4*6 GridPane 中可调整大小的按钮

Resizable Buttons in 4*6 GridPane

我的目标是在 JavaFX 中构建一个 Window,其中将有 24 个按钮,这样:

The problem I've right now is regarding the button size, I was able to increase the button size using setMinHeight and setMaxHeight However It's not responsive incase of window size change.

我的代码-

   private Button getSOSButton(){
    //create button

    Button b = new Button();
     b.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE)
    return b;
};

I've tried b.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE) inside of the loop but it's not working.

    int i=0;
    int j=0;
    GridPane gridPane = new GridPane();
    for(i=0;i<6;i++) {
        for (j = 0; j < 4; j++) {
            gridPane.add(getSOSButton(), i, j, 1, 1);
        }
    }
    gridPane.setPadding(new Insets(25,0,0,0));
    gridPane.setAlignment(Pos.CENTER);

        

    StackPane layout = new StackPane();
    layout.getChildren().addAll(hbox,gridPane);
    Scene scene = new Scene(layout, 600, 800);
    stage.setScene(scene);
    stage.show();

这是我的预期输出 -

这是我的代码结果 -

我更改了检查的第二个示例 here to make the button grow; resize the stage to see the effect. Noting the GridPane 可选布局约束,我允许每个 Button 始终在其网格单元的中心任意增长。

button.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
…
GridPane.setHalignment(gb, HPos.CENTER);
GridPane.setHgrow(gb, Priority.ALWAYS);
GridPane.setValignment(gb, VPos.CENTER);
GridPane.setVgrow(gb, Priority.ALWAYS);

import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.VPos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.stage.Stage;

/** @see  */
public class GridButtonTest extends Application {

    private static final int N = 5;
    private final List<List<Button>> list = new ArrayList<>();

    private Button getGridButton(int r, int c) {
        return list.get(r).get(c);
    }

    private Button createGridButton(int row, int col) {
        Button button = new Button("r" + row + ",c" + col);
        button.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
        button.setOnAction((ActionEvent event) -> {
            System.out.println(event.getSource() == getGridButton(row, col));
        });
        return button;
    }

    @Override
    public void start(Stage stage) {
        stage.setTitle("GridButtonTest");
        GridPane root = new GridPane();
        for (int row = 0; row < N - 1; row++) {
            list.add(new ArrayList<>());
            for (int col = 0; col < N + 1; col++) {
                Button gb = createGridButton(row, col);
                list.get(row).add(gb);
                root.add(gb, row, col);
                //GridPane.setMargin(gb, new Insets(N));
                GridPane.setHalignment(gb, HPos.CENTER);
                GridPane.setHgrow(gb, Priority.ALWAYS);
                GridPane.setValignment(gb, VPos.CENTER);
                GridPane.setVgrow(gb, Priority.ALWAYS);
            }
        }
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}