Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.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 使用动态添加的textfield值进行操作_Java_Swing - Fatal编程技术网

Java 使用动态添加的textfield值进行操作

Java 使用动态添加的textfield值进行操作,java,swing,Java,Swing,jPanel由5个jTextFields组成。我通过addButton动态添加jPanels。并通过saveButton检索动态添加的jPanel值。保存按钮的代码: private void writefile(JPanel panel_name, PrintWriter file){ Component[] children = panel_name.getComponents(); for (Component sp :

jPanel
由5个
jTextFields
组成。我通过
addButton
动态添加
jPanels
。并通过
saveButton
检索动态添加的
jPanel
值。
保存按钮的代码

private void writefile(JPanel panel_name, PrintWriter file){
        Component[] children = panel_name.getComponents();                   
        for (Component sp : children) {
            if (sp instanceof subPanel) {
                Component[] spChildren = ((subPanel)sp).getComponents();
                  int count=1;
                for (Component spChild : spChildren) {
                    if (spChild instanceof JTextField) {

                    String text = "";
                    if(count==1)
                    text=((JTextField)spChild).getText();

                    if(count==2)
                    text=((JTextField)spChild).getText();

                    if(count==4)
                    text=((JTextField)spChild).getText();

                    if(count==5)
                    text=((JTextField)spChild).getText();

                    if(count==3)
                    text=((JTextField)spChild).getText();

                    if(text.equals(null) || text.equals("")) {

                    }
                       else{
                    file.write(text);       
                        }                                               
                    }
                }
            }
        }
我已经在jTextFields中输入了
1,2,4,5,3
,当我单击Save按钮时,输出的jTextField值为:
1,2,3,4,5

即使我在
count==2
之后写入
count==4
,无论如何,我得到的输出都是有序的。我想问一下,在
count==2
之后,如何获得
count==4
的值?

您能否完成代码示例<例如,代码>计数不会在我能看到的任何地方递增。这不是您的确切代码。
count
的值是如何变化的?总是一样的!(组件spChild:spChildren)的
for
按顺序获取所有JTextFields,从而获得该输出。给出准确的代码,然后我们可以提供帮助。此外,您在所有检查中的代码,即
count==1
count==2
等都是相同的。那么,您如何期望不同的输出呢!我已经解决了我的问题。我完全改变了代码,使用getter方法,一切变得更加简单。