Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.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
Java 在循环中创建Swing组件并访问它们_Java_Swing_Loops - Fatal编程技术网

Java 在循环中创建Swing组件并访问它们

Java 在循环中创建Swing组件并访问它们,java,swing,loops,Java,Swing,Loops,我需要读取一个配置文件并将数据(它由键值对组成)放入一些文本字段中,这些文本字段必须在JFrame中动态创建。 之后,我想修改文本字段并再次保存对文件的更改 到目前为止,我所拥有的: FileConfiguration fileConfig = new PropertiesConfiguration(new File("xesfile.properties")); Iterator<String> keys = fileConfig.getKeys(

我需要读取一个配置文件并将数据(它由键值对组成)放入一些文本字段中,这些文本字段必须在JFrame中动态创建。 之后,我想修改文本字段并再次保存对文件的更改

到目前为止,我所拥有的:

        FileConfiguration fileConfig = new PropertiesConfiguration(new File("xesfile.properties"));

        Iterator<String> keys = fileConfig.getKeys();
        while (keys.hasNext()) {
            String singleKey = keys.next();

            //for the case that a key has multiple values
            if (fileConfig.getProperty(singleKey).getClass().equals(ArrayList.class)) {
                ArrayList<String> blaArray = (ArrayList<String>) fileConfig.getProperty(singleKey);
                for (int i = 0; i < blaArray.size(); i++) {
                    this.keyLabel = new JLabel(this.nameKeyLabel + this.count);
                    this.entityLabel = new JLabel(this.nameEntityLabel + this.count);
                    this.keyTextField = new JTextField();
                    this.keyTextField.setName(this.nameKeyTextField + this.count);
                    this.keyTextField.setText(singleKey);
                    this.entityTextField = new JTextField();
                    this.entityTextField.setName(this.nameEntityTextField + this.count);
                    this.entityTextField.setText(blaArray.get(i));
                    this.count++;
                    this.configFrame.add(this.keyLabel);
                    this.configFrame.add(this.keyTextField);
                    this.configFrame.add(this.entityLabel);
                    this.configFrame.add(this.entityTextField);
                    this.configFrame.revalidate();
                    this.configFrame.repaint();
                    this.configFrame.pack();
                }
            //for the case a key has a single value
            } else {
                this.keyLabel = new JLabel(this.nameKeyLabel + this.count);
                this.entityLabel = new JLabel(this.nameEntityLabel + this.count);
                this.keyTextField = new JTextField();
                this.keyTextField.setName(this.nameKeyTextField + this.count);
                this.keyTextField.setText(singleKey);
                this.entityTextField = new JTextField();
                this.entityTextField.setName(this.nameEntityTextField + this.count);
                this.entityTextField.setText((String) fileConfig.getProperty(singleKey));
                this.count++;
                this.configFrame.add(this.keyLabel);
                this.configFrame.add(this.keyTextField);
                this.configFrame.add(this.entityLabel);
                this.configFrame.add(this.entityTextField);
                this.configFrame.revalidate();
                this.configFrame.repaint();
                this.configFrame.pack();
            }

        }
FileConfiguration fileConfig=新属性配置(新文件(“xesfile.properties”);
迭代器keys=fileConfig.getKeys();
while(keys.hasNext()){
字符串singleKey=keys.next();
//对于键具有多个值的情况
if(fileConfig.getProperty(singleKey.getClass().equals(ArrayList.class)){
ArrayList blaArray=(ArrayList)fileConfig.getProperty(singleKey);
对于(int i=0;i
正如您所见,文本字段在while循环的每个过程中都会一次又一次地创建。但是我需要访问我在第一次循环中创建的文本字段。 我想如果我使用
getName()
访问文本字段,它们会被返回,但事实并非如此。
有人能帮我吗?

将您的文本字段存储在一个映射中,键作为名称,值作为文本字段对象本身。您可以稍后使用以下方式访问它:

TextField textField = map.get("myField");
这很简单

// In loop
1) Create the component.
2) Add it to an Generic ArrayList
3) You can also attach an actionListener to the component in loop if you want.
// Out of loop
4) Iterate the ArrayList and access the desire component.

非常感谢。这真的很简单,但我对java编程相当陌生。把你的答案标记为正确。我不明白为什么我被否决了…很高兴它帮助了你。。是的,连我都不明白为什么它被否决了。