【问题标题】:Create Swing Components in a loop and access them在循环中创建 Swing 组件并访问它们
【发布时间】:2014-11-28 15:48:53
【问题描述】:

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

到目前为止我所拥有的:

        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();
            }

        }

正如您所见,TextField 会在 while 循环的每一次传递中一次又一次地创建。但是我需要访问我在该示例的循环的第一遍中创建的文本字段。 想如果我会用getName() 访问TextFields,它们会被返回,但事实并非如此。 谁能帮帮我?

【问题讨论】:

    标签: java swing loops


    【解决方案1】:

    将您的 TextField 存储在 Map 中,键作为名称,值作为 TextField 对象本身。您可以稍后使用:

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

    【讨论】:

    • 谢谢。这真的很简单,但我对 java 编程很陌生。将您的答案标记为正确。不明白为什么我被否决了...
    • 很高兴它对您有所帮助.. 是的,即使我不明白为什么它被否决了。
    【解决方案2】:

    这很简单

    // 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.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-16
      • 1970-01-01
      • 2011-01-03
      • 2019-02-16
      • 1970-01-01
      • 2021-11-13
      相关资源
      最近更新 更多