如何为循环中创建的100个文本字段创建changeListener?

时间:2017-02-20 05:29:40

标签: java javafx changelistener

我创建了100个文本字段并将它们放入gridPane中。用户可以更改任何文本字段中的值。我需要能够注册该更改的值,以便我的按钮找到最大尺寸子方块将正常工作。因为文本字段是在循环中创建的,所以我不知道如何在任何单个文本字段中监听更改。

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Paint;
import javafx.stage.Stage;

import static java.lang.Math.rint;


public class Exercise22_19 extends Application {
    //declare a gridpane here so that methods have easy access
    GridPane gp1;
    int[][] valList;
    @Override
    public void start(Stage primaryStage) {

        //stack pane to hold the gridpane; makes it easy to keep the grid
        //centered in the stage
        StackPane root = new StackPane();

        ///gridpane to hold the 10x10 matrix of textfields
        gp1 = new GridPane();
        gp1.setPadding(new Insets(5, 5, 5, 5));
        //center the gridpane in the stackpane
        gp1.setAlignment(Pos.CENTER);
        root.getChildren().add(gp1);
        setNumbers();

        //refresh button: refills matrix with new values
        Button refreshBt = new Button("Refresh");
        //call the function to reset the values in the matrix when the refresh
        //button is clicked
        refreshBt.setOnAction(e -> setNumbers());

        //findLargestBlock button finds the largest square block of 1's in the
        //matrix
        Button lrgBlkBt = new Button("Find Largest Button");
        //call the function to find the largest square block of 1's when the
        //find button is clicked
        lrgBlkBt.setOnAction(e -> findBigBlock());

        //makes a space between the last row of numbers and the buttons. Even
        //though it is onle set on one button, it affects the entire row
        GridPane.setMargin(refreshBt, new Insets(5, 5, 5, 5));

        //add buttons to gridpane
        //refresh spans 3 columns
        gp1.add(refreshBt, 1, 10, 3, 1);
        //find spans 6 columns
        gp1.add(lrgBlkBt, 4, 10, 6, 1);

        Scene scene = new Scene(root);
        primaryStage.setTitle("Exercise 22_19 (Dynamic)");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    //set all textfield values in the matrix to 0 or 1
    public void setNumbers() {
        //gridpane does not have a method to access the value of a node in the
        //gridpane, so I will store the values in an array.
        valList = new int[10][10];
        for (int i = 0; i < 10; i++) {
            for (int k = 0; k < 10; k++) {
                TextField tf = new TextField();
                tf.setPrefWidth(25);
                //set the value of the text field to 0 or 1
                //create a random 0 or 1 integer
                int val = (int)(rint(Math.random()));
                //fill the array with the values
                valList[i][k] = val;
                String valStr = String.valueOf(val);
                tf.setText(valStr);
                //add text fields to gridpane
                GridPane.setConstraints(tf, k, i);
                gp1.getChildren().add(tf);
            }

        }
    }

    //find the largest square block of 1's in the matrix
    public void findBigBlock() {
        //size of square matrix
        int n = 10;
        //max tells the size of the largest square sub-maxtrix, e.g. if max==3,
        //the largest sub-matrix is a 3x3 matrix
        int max = 0;
        int maxRow = 0;
        int maxCol = 0;
        //color for the textbox; red
        Paint tfColor = Paint.valueOf("FF0000");

        int[][] arr = new int[10][10];
        //copy first row to new array
        System.arraycopy(valList[0], 0, arr[0], 0, 10);
        //copy first col to new array
        for (int i = 0; i < n; i++) {
            arr[i][0] = valList[i][0];
        }

        //determines the size and location of the maximum sub-square
        for (int row = 1; row < n; row++) {
            for (int col = 1; col < n; col++) {
                if (valList[row][col] == 1) {
                    arr[row][col] =((Math.min(valList[row][col-1],
                            (Math.min(valList[row-1][col], valList[row-1][col-1])))) + 1);
                    if (arr[row][col] >= max) {
                        max = arr[row][col];
                        maxRow = row;
                        maxCol = col;
                    }
                }
                else
                    arr[row][col] = 0;
            }
        }

        //replaces the normal textfields with red textfields for the maximum
        //sub-square
        for (int i = max; i > 0; i--) {
            for (int j = maxRow; j > maxRow - max; j--) {
                for (int k = maxCol; k > maxCol - max; k--) {
                    //create a nex textfield
                    TextField tf = new TextField();
                    tf.setPrefWidth(25);
                    //set color to red
                    tf.setStyle("-fx-control-inner-background: #"+tfColor.toString().substring(2));
                    tf.setText(String.valueOf(valList[j][k]));
                    //put the red textfield into the gridpane
                    GridPane.setConstraints(tf, k, j);
                    gp1.getChildren().add(tf);
                }
            }
        }
    }

    public static void main(String[] args) {
        Application.launch(args);
    }
}

1 个答案:

答案 0 :(得分:1)

这样的事情会适用于你的情况:

 public void setNumbers() {
    //gridpane does not have a method to access the value of a node in the
    //gridpane, so I will store the values in an array.
    valList = new int[10][10];
    for (int i = 0; i < 10; i++) {
        for (int k = 0; k < 10; k++) {
            TextField tf = new TextField();
            tf.setPrefWidth(25);
            //set the value of the text field to 0 or 1
            //create a random 0 or 1 integer
            int val = (int)(rint(Math.random()));
            //fill the array with the values
            valList[i][k] = val;
            String valStr = String.valueOf(val);
            tf.setText(valStr);
            //add text fields to gridpane
            GridPane.setConstraints(tf, k, i);
            gp1.getChildren().add(tf);

            tf.textProperty().addListener((observable, oldValue, newValue) -> {
                 valList[GridPane.getRowIndex(tf)][GridPane.getColumnIndex(tf)] = Integer.parseInt(newValue);
             });
         }

     }
 }

请注意,您应该为so

等字段添加类型检查