JavaFX:如何从GridPane中的dynimically创建的TextFields的值中计算平均值? [英] JavaFX: How to calulcate average from the values of dynimically created TextFields inside GridPane?

查看:126
本文介绍了JavaFX:如何从GridPane中的dynimically创建的TextFields的值中计算平均值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在JavaFx中创建一个应用程序,我正在创建两个具有相同行数的两个GridPane,两个GridPanes都有文本字段,下面有一个这样的Button:

I'm creating an application in JavaFx in which I'm creating two GridPanes dynimically with same number of rows, both GridPanes have textfields and below there is a Button like this:

用户可以在GridPane A中编写任意数字动态创建的TextFields,如下所示:

User can write any number in GridPane A's dynamically created TextFields like this:

我想要的是当用户按下提交按钮程序时应该计算每行中存在的值之和,然后将每行计算的总和除以整个GridPane的TextFields总和,并根据GridPane在第二个GridPane的TextFields中显示结果行位置,例如GridPane第0行计算结果应为显示如下所示:在GridPane B第0行中:

What I want is when user will press Submit button program should calculate sum of values present in each row and then divide each row calculated sum with whole GridPane's TextFields sum and display the result in second GridPane's TextFields according GridPane A row position e.g GridPane A row 0 calculation result should be displayed in GridPane B row 0 like this:

我正在创建类似GridPane的GridPane A:

I'm creating GridPane like this 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;
}

GridPane B

GridPane 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;
}

从特定行和列的表返回组件的方法:

Method to return component from table at specific row and column:

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");

    }


推荐答案

我认为你的方法是以正确的方式实现,你可以尝试这个,当点击按钮时(,用它的动作监听器包裹它)( NB。未经测试 ):

I think your approach is on the right way to achieve that, you may try this, when the button is clicked (i.e wrap it with its action listener) (NB. Untested):

// pass the numberOfRows which is comboxBox.getValue()
// pass tableA and tableB.
// fetch the numbers (in a String format) from all TextFields at every row in tableA
// and caclulate the result after parsing the Strings from each as double values
// set the result in the corresponding TextField in tableB in every loop
private void calcul(GridePane tableA, GridPane tableB, int numberOfRows) {
     double result = 0;
     for(int i=0 ; i<numberOfRows; i++){
        result = (Double.parseDouble(((TextField) getComponent (i, 0, tableA)).getText()) +
                  Double.parseDouble(((TextField) getComponent (i, 1, tableA)).getText()) +
                  Double.parseDouble(((TextField) getComponent (i, 2, tableA)).getText()))
                                            / (numberOfRows*3);

        ((TextField) getComponent (i, 0, tableB)).setText(String.valueOf(result));

     }
}

UPDATE

OP在以下评论中提供了更多解释后,需要对上述代码进行一些调整:

After OP provided more explanation in the below comments, a few adjustments the above code needs:

// pass the numberOfRows which is comboxBox.getValue()
// pass tableA and tableB.
private void calcul(GridePane tableA, GridPane tableB, int numberOfRows) {
    // first get the total and store it in a variable
    double total =0;
    for(Node node : tableA){
      if(node instanceof TextField){
         total += Double.parseDouble(((TextField)node).getText());
      }
    }

    // fetch the numbers (in a String format) from all TextFields at every row in tableA
    // and calculate the average after parsing the Strings from each as double values
    // set the average in the corresponding TextField in tableB in every loop
    double average = 0;
    for(int i=0 ; i<numberOfRows; i++){
       average = (Double.parseDouble(((TextField) getComponent (i, 0, tableA)).getText()) +
                  Double.parseDouble(((TextField) getComponent (i, 1, tableA)).getText()) +
                  Double.parseDouble(((TextField) getComponent (i, 2, tableA)).getText()))
                                           / (total);

       ((TextField) getComponent (i, 0, tableB)).setText(String.valueOf(average));

    }
}

这篇关于JavaFX:如何从GridPane中的dynimically创建的TextFields的值中计算平均值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆