简体   繁体   中英

Resizable Buttons in 4*6 GridPane

My target is to build a Window in JavaFX where there will be 24 buttons, such way that:

  • The buttons should be placed in 4x6 grid (meaning 4 columns, 6 rows)
  • Edit the grid and buttons in a way that allows the grid to fill the whole window (even when resizing)

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.

My code-

   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();

This is my expected output - 这是我的预期输出

This is my code result - 这是我的代码结果

I altered the second example examined here to make the button grow; resize the stage to see the effect. Noting the GridPane optional layout constraints, I allowed each Button to always grow arbitrarily in the center of its grid cell.

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 https://stackoverflow.com/a/69429741/230513 */
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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM