【问题标题】:Refering to a textfield in Gridpane引用 Gridpane 中的文本字段
【发布时间】:2017-03-03 23:31:10
【问题描述】:

好的。因此,给你们一些背景信息:我正在编写数独,没有过多地使用 GUI,但做了很多研究。

我创建了一个 2 for 循环,创建了一个 9x9 Gridpane,其中填充了以下代码的文本字段

GridPane grid = new GridPane();

for(int x =0; x < 9; x++){
    for(int y= 0; y < 9; y++){
        TextField textField = new TextField("0");
        textField.setStyle("-fx-pref-width: 2em;");
        GridPane.setConstraints(textField, y, x);
        grid.getChildren().add(textField);
    }
}

现在我要做的就是有一个清晰的取值方式——例如坐标 [2][5] 返回二维数组并与答题纸进行比较。我的问题是在我的网格窗格中指定一个坐标。

最好的问候。

【问题讨论】:

    标签: javafx textfield gridpane


    【解决方案1】:

    将文本字段放入数组中:

    GridPane grid = new GridPane();
    TextField[][] textFields = new TextField[9][9] ;
    
    for(int x =0; x < 9; x++){
        for(int y= 0; y < 9; y++){
            TextField textField = new TextField("0");
            textField.setStyle("-fx-pref-width: 2em;");
            GridPane.setConstraints(textField, y, x);
            grid.getChildren().add(textField);
            textFields[x][y]=textField ;
        }
    }
    

    然后您可以使用textFields[x][y].getText() 访问任何元素(x,y)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-20
      • 2016-04-26
      • 1970-01-01
      • 2016-10-23
      • 2014-10-26
      • 2014-11-01
      • 1970-01-01
      相关资源
      最近更新 更多