Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaFX:在HashMap中存储TextField_Javafx_Hashmap_Textfield - Fatal编程技术网

JavaFX:在HashMap中存储TextField

JavaFX:在HashMap中存储TextField,javafx,hashmap,textfield,Javafx,Hashmap,Textfield,尝试在HashMap中存储TextField,如下所示: TextField textfield; Map<String, TextField> map = new HashMap<String, TextField>(); for(int row = 0; row < RowLim; row++) { for(int col = 0; col < ColLim; col++) { Str

尝试在HashMap中存储TextField,如下所示:

TextField textfield;
Map<String, TextField> map = new HashMap<String, TextField>();
        for(int row = 0; row < RowLim; row++) {
            for(int col = 0; col < ColLim; col++) {
                String name = row+""+col;
                map.put(name,textfield = new TextField());
            }
        }
TextField TextField;
Map Map=newhashmap();
对于(int row=0;row

这是一个数独解算器,旨在教我一些关于JavaFX的知识。每个文本字段都显示在网格窗格中,并且应该有一个与其在地图中的位置相对应的键。然而,当我在任何文本字段中输入并打印它时,我得到一个空值。我不认为我对textfield的工作原理有正确的想法,但我真的看不到任何其他解决方案,所以我被卡住了。打印时我使用textfield.getText()方法

我最终解决了这个问题,添加了一个新的HashMap来检索每个textfield.getText()值并将其存储为字符串。添加try-catch语句以排除所有null语句。它工作得很好。这是我的密码:

Map<String, String> numbermap = new HashMap<String, String>();
        for(Map.Entry<String,TextField> me : map.entrySet()) {
            numbermap.put(me.getKey(),me.getValue().getText());
        }
        try {
            for(Map.Entry<String,String> me : numbermap.entrySet()) {
                //Code to use inputvalues from each textfield...
            }

        } catch (NullPointerException e) {
            System.out.println("Gives null");
        }
Map numbermap=newhashmap();
对于(Map.Entry me:Map.entrySet()){
numbermap.put(me.getKey(),me.getValue().getText());
}
试一试{
对于(Map.Entry me:numbermap.entrySet()){
//使用每个文本字段的输入值的代码。。。
}
}捕获(NullPointerException e){
System.out.println(“给出空值”);
}