简体   繁体   English

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

[英]Resizable Buttons in 4*6 GridPane

My target is to build a Window in JavaFX where there will be 24 buttons, such way that:我的目标是在 JavaFX 中构建一个 Window ,其中将有 24 个按钮,这样:

  • The buttons should be placed in 4x6 grid (meaning 4 columns, 6 rows)按钮应放置在 4x6 网格中(表示 4 列,6 行)
  • Edit the grid and buttons in a way that allows the grid to fill the whole window (even when resizing)以允许网格填充整个 window 的方式编辑网格和按钮(即使在调整大小时)

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.我现在遇到的问题是关于按钮大小,我能够使用 setMinHeight 和 setMaxHeight 增加按钮大小但是它没有响应 window 大小变化的情况。

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.我已经在循环内尝试了 b.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE) 但它不起作用。

    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 -这是我预期的 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.注意到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 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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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