JavaFX网格窗格按钮“获取”2个单元格

时间:2018-02-28 11:38:39

标签: javafx

我正在尝试使用JavaFX制作类似GUI的计算器。 问题是我不知道添加按钮以“取”两个单元格的方法。见下面的照片。

在那个空的空间里,我想添加一个按钮 Calc

我尝试将它添加到位置0,3,但它会将所有按钮空白

这是我的代码

主:

public class Test extends Application {
    @Override
    public void start(Stage primaryStage) {
        BorderPane borderPane = new BorderPane();
        borderPane.setPadding(new Insets(5, 35, 5, 35));

        TextField txf = new TextField("6.333");
        txf.setAlignment(Pos.TOP_RIGHT);
        txf.setPrefHeight(35);

        GridPane grid = new GridPane();
        grid.setHgap(5);
        grid.setVgap(5);
        grid.setPadding(new Insets(10, 5, 10, 5));

        String[] signs = new String[]{"7", "8", "9", "+",
                                      "4", "5", "6", "-",
                                      "3", "2", "1", "*"};
        int signIndex = 0;

        for(int i = 0; i<3; i++) {
            for(int j = 0; j<4; j++) {
                CustomButton btnTemp = new CustomButton(signs[signIndex]);
                grid.add(btnTemp, j, i);
                signIndex++;
            }
        }

        CustomButton btnZero = new CustomButton("0", 100, 20);
        //grid.add(btnZero, 0, 3);

        CustomButton btnDot = new CustomButton(".");
        grid.add(btnDot, 2, 3);
        CustomButton btnSlash = new CustomButton("/");
        grid.add(btnSlash, 3, 3);

        CustomButton btnC = new CustomButton("C", 100, 20);
        grid.add(btnC, 4, 0);
        CustomButton btnSqrt = new CustomButton("√", 100, 20);
        grid.add(btnSqrt, 4, 1);
        CustomButton btnPlusMinus = new CustomButton("+/-", 100, 20);
        grid.add(btnPlusMinus, 4, 2);
        CustomButton btnEqu = new CustomButton("=", 100, 20);
        grid.add(btnEqu, 4, 3);

        borderPane.setTop(txf);
        borderPane.setCenter(grid);

        Scene scene = new Scene(borderPane, 400, 200);

        primaryStage.setTitle("Calculator");
        primaryStage.setScene(scene);
        primaryStage.setResizable(false);
        primaryStage.show();
    }
}

1 个答案:

答案 0 :(得分:2)

使用setColumnSpan

GridPane.setColumnSpan(btnC, 2); 

(假设你的意思是btnC,我无法理解您希望跨越2列的哪个按钮......)