我創建在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