有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我需要读取一个配置文件并将数据(它由键值对组成)放入一些文本字段中,这些文本字段必须在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();
            }

        }

正如您所见,文本字段在while循环的每个过程中都会一次又一次地创建。但是我需要访问我在第一次循环中创建的文本字段。 我想如果我用getName()访问文本字段,它们会被返回,但事实并非如此。 有人能帮我吗


共 (2) 个答案

  1. # 1 楼答案

    这太简单了

    // 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.
    
  2. # 2 楼答案

    将TextField存储在地图中,键作为名称,值作为TextField对象本身。您可以稍后使用以下方式访问它:

    TextField textField = map.get("myField");