JavaFX Grid Pane按钮“获取”2个单元格
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaFX Grid Pane按钮“获取”2个单元格相关的知识,希望对你有一定的参考价值。
我正在尝试使用JavaFX制作类似GUI的计算器。问题是我不知道添加按钮以“取”两个单元格的方法。见下面的照片。
在那个空的空间我想添加一个按钮
我尝试将它添加到位置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();
}
}
答案
GridPane.setColumnSpan(btnC, 2);
(假设你的意思是btnC
,我无法理解你想要跨越2列的哪个按钮...)
以上是关于JavaFX Grid Pane按钮“获取”2个单元格的主要内容,如果未能解决你的问题,请参考以下文章