【发布时间】:2017-06-15 18:32:02
【问题描述】:
我正在 JavaFx 中创建一个应用程序,其中我正在动态创建两个具有相同行数的 GridPanes,两个 GridPanes 都有文本字段,并且下面有一个这样的按钮:
用户可以像这样在 GridPane A 的动态创建的 TextField 中写入任意数字:
我想要的是当用户按下提交按钮时,程序应该计算每行中存在的值的总和,然后将每一行计算的总和除以整个 GridPane 的 TextFields 总和,并根据 GridPane 在第二个 GridPane 的 TextFields 中显示结果 A 行位置,例如 GridPane第 0 行的计算结果应该显示在 GridPane B 第 0 行中,如下所示:
我正在创建这样的 GridPane A:
public static GridPane tableA(int rows, Button button){
GridPane table = new GridPane();
rowValidationBindings = new BooleanBinding[rows];
for(int i=0; i<rows; i++){
TextField textField1 = new TextField();
textField1.setAlignment(Pos.CENTER);
TextField textField2 = new TextField();
textField2.setAlignment(Pos.CENTER);
TextField textField3 = new TextField();
textField3.setAlignment(Pos.CENTER);
rowValidationBindings[i] = Bindings.createBooleanBinding(
() -> {
if (textField1.getText().matches("\\d+") &&
textField2.getText().matches("\\d+") &&
textField1.getText().matches("\\d+") {
return true ;
} else {
return false ;
}
}, textField1.textProperty(), textField2.textProperty(), textField3.textProperty()
);
table.add(textField1, 0, i+1);
table.add(textField2, 1, i+1);
table.add(textField3, 2, i+1);
}
button.disableProperty().bind(Bindings.createBooleanBinding(
() -> ! Stream.of(rowValidationBindings).allMatch(BooleanBinding::get),
rowValidationBindings
));
return table;
}
网格窗格 B
public static GridPane tableB(int rows, Button button){
GridPane table = new GridPane();
rowValidationBindings = new BooleanBinding[rows];
for(int i=0; i<rows; i++){
TextField textField1 = new TextField();
textField1.setAlignment(Pos.CENTER);
rowValidationBindings[i] = Bindings.createBooleanBinding(
() -> {
if (textField1.getText().matches("\\d+") {
return true ;
} else {
return false ;
}
}, textField1.textProperty()
);
table.add(textField1, 0, i+1);
}
button.disableProperty().bind(Bindings.createBooleanBinding(
() -> ! Stream.of(rowValidationBindings).allMatch(BooleanBinding::get),
rowValidationBindings
));
return table;
}
从表中特定行和列返回组件的方法:
public static Node getComponent (int row, int column, GridPane table) {
for (Node component : table.getChildren()) { // loop through every node in the table
if(GridPane.getRowIndex(component) == row &&
GridPane.getColumnIndex(component) == column) {
return component;
}
}
return null;
}
按钮点击实现:
@FXML
private void calcul() {
GridPane table = (GridPane) anchorPane.getChildren().get(0);
for(int i=1 ; i<=comboxBox.getValue(); i++){
String text0 = ((TextField) getComponent (i, 0, table)).getText();
String text1 = ((TextField) getComponent (i, 1, table)).getText();
String text2 = ((TextField) getComponent (i, 2, table)).getText();
System.out.println(text0 + " " + text1 + " " + text2);
System.out.println("Next Row");
}
【问题讨论】:
-
我们很清楚您需要什么,但我认为您没有告诉我们问题所在......
-
我很确定几天前已经回答了这个问题。
-
@SedrickJefferson 如果它已经回答了,你可以在这里标记已经回答的问题吗?
标签: java javafx javafx-2 javafx-8