我创建在JavaFX中,我创建了两个GridPanes应用dynimically具有相同的行数,都GridPanes具有文本字段和下面有这样一个按钮:JavaFX:如何从GridPane内的动态创建的TextField的值来平均平均值?
用户可以写任何数量的GridPane A的动态创建的TextField这样的:
我要的是,当用户将按下提交按钮的程序应该算目前各行中值的总和,然后将每个RO瓦特与全GridPane的TextField的总和计算出的和,并显示结果在根据GridPane甲行位置例如GridPane甲行0的计算结果第二GridPane的TextField的应显示在GridPane乙行0是这样的:
I”米这样GridPane甲创建GridPane:
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;
}
GridPane乙
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");
}
我们很清楚自己需要什么,但我不认为你已经告诉我们问题是什么... – Jai
我很确定这是几天前回答的。 – Sedrick
@SedrickJefferson如果它已经回答了,请您在此标记已经回答的问题? – Junaid