【问题标题】:Adding JTextFields hard code free免费添加 JTextFields 硬代码
【发布时间】:2012-11-09 04:19:26
【问题描述】:

我正在尝试为一个程序编写代码,该程序将为用户提供从上到下的硬编码数量的 JTextField,并可以选择添加额外的 JTextField。之后我必须从每个 JTextField 中检索用户导入的数据。我该怎么做呢? 所有 JTextField 都添加到 JPanel。

【问题讨论】:

  • 好吧,我建议创建一个硬编码JTextFields 的数组列表,然后将任何新创建的文本字段添加到数组列表中。然后只需迭代列表并在需要时获取每个文本或其他内容

标签: java swing jtextfield


【解决方案1】:

做这样的事情:

    ArrayList<JTextField> list = new ArrayList<JTextField>();
    //add all the hardcoded JTextFields to this list
    //b is a button which the user clicks to add extra TextFields
    b.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            JTextField a = new JTextField();
            panel.add(a);//panel is the existing panel
            list.add(a);
        }
    });
}

因此,在“列表”的帮助下,您以后可以检索所有文本字段

【讨论】:

  • @user1840352 如果回复正常,请点赞并采纳
  • 如何添加代码块,我想告诉你我做了什么,但我想不通。
猜你喜欢
  • 2016-10-31
  • 1970-01-01
  • 1970-01-01
  • 2017-01-08
  • 1970-01-01
  • 2013-01-10
  • 2011-02-12
  • 2018-01-01
  • 1970-01-01
相关资源
最近更新 更多