Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.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 动态添加JTextFields后删除它_Java_Swing_Jlabel_Jtextfield - Fatal编程技术网

Java 动态添加JTextFields后删除它

Java 动态添加JTextFields后删除它,java,swing,jlabel,jtextfield,Java,Swing,Jlabel,Jtextfield,我的目标是通过单击JLabel同时删除两个JTextField 我创建了如下文本字段: public void mouseClicked(MouseEvent e) { inc++; txtName= new JTextField(); txtNumber = new JTextField(); txtName.setName("txtName"+inc); txtNumb

我的目标是通过单击JLabel同时删除两个JTextField

我创建了如下文本字段:

public void mouseClicked(MouseEvent e) {


            inc++;
            txtName= new JTextField();
            txtNumber = new JTextField();
            txtName.setName("txtName"+inc);
            txtNumber.setName("txtNumber" + inc);

            pnlPanel.add(txtName);
            pnlPanel.add(txtNumber);

            if(count>0){
                x+=50;
                y+=50;

                txtName.setBounds(225,6+y, 182, 27);
                txtNumber.setBounds(35, 6+y, 182, 27);
                txtName.setName(tempBox+count);
                if(pnlTxtText.getComponentCount() >9){

                    pnlPanel.setPreferredSize(new Dimension(450+y,50+y));
                    pnlPanel.add(txtStudName);
                    pnlPanel.add(txtStudentNumber);

                    frmFrame.repaint();
                    scrpPanel.revalidate();
                }
            }
            frmFrame.repaint();
        }

    });
这是我删除文本字段的代码:

public void mouseClicked(MouseEvent e) {
    int countPlace= pnlPanel.getComponentCount();
    int countOfRemaining =countPlace;
    pnlPanel.remove(--countOfRemaining);
    frmFrame.revalidate();
    pnlPanel.remove(--countOfRemaining);
    frmFrame.revalidate();
}
}))

它不是删除同一行上的txtfields,而是一个接一个地删除它,我不希望这样。请帮帮我。多谢各位


重新绘制()之前调用
pnlPanel.revalidate()


不要使用setBounds()。改为定义porper
LayoutManager

repaint()之前调用
pnlPanel.revalidate()


不要使用setBounds()。改为定义porper
LayoutManager

repaint()之前调用
pnlPanel.revalidate()


不要使用setBounds()。改为定义porper
LayoutManager

repaint()之前调用
pnlPanel.revalidate()

不要使用setBounds()。定义porper
LayoutManager

而不是这个

public void mouseClicked(MouseEvent e) {
        int countPlace= pnlPanel.getComponentCount();
        int countOfRemaining =countPlace;
        pnlPanel.remove(countOfRemaining-1);
        frmFrame.repaint();
        pnlPanel.remove(countOfRemaining-1);
        frmFrame.repaint();
    }
});
用这个

public void mouseClicked(MouseEvent e) {
        int countPlace= pnlPanel.getComponentCount();
        int countOfRemaining =countPlace;
        pnlPanel.remove(--countOfRemaining);
        frmFrame.revalidate();
        pnlPanel.remove(--countOfRemaining);
        frmFrame.revalidate();
    }
});
上面的一个抛出ArrayIndeOutOfBounds异常,因为移除组件后剩余计数变量没有递减。因此,当您第二次尝试删除时,索引将超出范围。

而不是此

public void mouseClicked(MouseEvent e) {
        int countPlace= pnlPanel.getComponentCount();
        int countOfRemaining =countPlace;
        pnlPanel.remove(countOfRemaining-1);
        frmFrame.repaint();
        pnlPanel.remove(countOfRemaining-1);
        frmFrame.repaint();
    }
});
用这个

public void mouseClicked(MouseEvent e) {
        int countPlace= pnlPanel.getComponentCount();
        int countOfRemaining =countPlace;
        pnlPanel.remove(--countOfRemaining);
        frmFrame.revalidate();
        pnlPanel.remove(--countOfRemaining);
        frmFrame.revalidate();
    }
});
上面的一个抛出ArrayIndeOutOfBounds异常,因为移除组件后剩余计数变量没有递减。因此,当您第二次尝试删除时,索引将超出范围。

而不是此

public void mouseClicked(MouseEvent e) {
        int countPlace= pnlPanel.getComponentCount();
        int countOfRemaining =countPlace;
        pnlPanel.remove(countOfRemaining-1);
        frmFrame.repaint();
        pnlPanel.remove(countOfRemaining-1);
        frmFrame.repaint();
    }
});
用这个

public void mouseClicked(MouseEvent e) {
        int countPlace= pnlPanel.getComponentCount();
        int countOfRemaining =countPlace;
        pnlPanel.remove(--countOfRemaining);
        frmFrame.revalidate();
        pnlPanel.remove(--countOfRemaining);
        frmFrame.revalidate();
    }
});
上面的一个抛出ArrayIndeOutOfBounds异常,因为移除组件后剩余计数变量没有递减。因此,当您第二次尝试删除时,索引将超出范围。

而不是此

public void mouseClicked(MouseEvent e) {
        int countPlace= pnlPanel.getComponentCount();
        int countOfRemaining =countPlace;
        pnlPanel.remove(countOfRemaining-1);
        frmFrame.repaint();
        pnlPanel.remove(countOfRemaining-1);
        frmFrame.repaint();
    }
});
用这个

public void mouseClicked(MouseEvent e) {
        int countPlace= pnlPanel.getComponentCount();
        int countOfRemaining =countPlace;
        pnlPanel.remove(--countOfRemaining);
        frmFrame.revalidate();
        pnlPanel.remove(--countOfRemaining);
        frmFrame.revalidate();
    }
});

上面的一个抛出ArrayIndeOutOfBounds异常,因为移除组件后剩余计数变量没有递减。因此,当您第二次尝试删除时,索引将超出范围。

我建议您以
的形式将所有
jpanel
添加到映射中(我使用HashMap)。按顺序命名它们,然后只需执行
Map.remove(Map.size()-1)
Map.remove(Map.size()-2)

通过执行
Map.keySet()
,还可以获得当前的整数集(键)

Map temp=newhashmap();
临时输入(0,新JPanel());
临时输入(1,新JPanel());
临时输入(2,新JPanel());
温度移除(温度大小()-1);

使维护多组面板变得更加容易。在我的应用程序中,我不使用数字来控制它们,而是使用短名称。

我建议您以
的形式将所有
jpanel
添加到映射中(我使用HashMap)。按顺序命名它们,然后只需执行
Map.remove(Map.size()-1)
Map.remove(Map.size()-2)

通过执行
Map.keySet()
,还可以获得当前的整数集(键)

Map temp=newhashmap();
临时输入(0,新JPanel());
临时输入(1,新JPanel());
临时输入(2,新JPanel());
温度移除(温度大小()-1);

使维护多组面板变得更加容易。在我的应用程序中,我不使用数字来控制它们,而是使用短名称。

我建议您以
的形式将所有
jpanel
添加到映射中(我使用HashMap)。按顺序命名它们,然后只需执行
Map.remove(Map.size()-1)
Map.remove(Map.size()-2)

通过执行
Map.keySet()
,还可以获得当前的整数集(键)

Map temp=newhashmap();
临时输入(0,新JPanel());
临时输入(1,新JPanel());
临时输入(2,新JPanel());
温度移除(温度大小()-1);

使维护多组面板变得更加容易。在我的应用程序中,我不使用数字来控制它们,而是使用短名称。

我建议您以
的形式将所有
jpanel
添加到映射中(我使用HashMap)。按顺序命名它们,然后只需执行
Map.remove(Map.size()-1)
Map.remove(Map.size()-2)

通过执行
Map.keySet()
,还可以获得当前的整数集(键)

Map temp=newhashmap();
临时输入(0,新JPanel());
临时输入(1,新JPanel());
临时输入(2,新JPanel());
温度移除(温度大小()-1);

使维护多组面板变得更加容易。在我的应用程序中,我不会用数字来控制它们,而是用短名称来控制它们。

;你好我添加了重新验证。我不明白的是为什么会有这样的代码:int countPlace=pnlTxtTxt.getComponentCount();int countofresaining=countPlace;pnlTxtTxt.remove(保留计数);frmGM.revalidate();frmGM.repaint();pnlTxtTxt.remove(保留计数-1);引发此异常:线程“AWT-EventQueue-0”java.lang.ArrayIndexOutOfBoundsException中的异常:数组索引超出范围:8 frmGM.revalidate();frmGM.repaint();你好我添加了重新验证。我不明白的是为什么会有这样的代码:int countPlace=pnlTxtTxt.getComponentCount();int countofresaining=countPlace;pnlTxtTxt.remove(保留计数);frmGM.revalidate();frmGM.repaint();pnlTxtTxt.remove(保留计数-1);引发此异常:线程“AWT-EventQueue-0”java.lang.ArrayIndexOutOfBoundsException中的异常:数组索引超出范围:8 frmGM.revalidate();frmGM.repaint();你好我添加了重新验证。我不明白的是为什么会有这样的代码:int countPlace=pnlTxtTxt.getComponentCount();int countofresaining=countPlace;pnlTxtTxt.remove(保留计数);frmGM.revalidate();frmGM.repaint();pnlTxtTxt.remove(保留计数-1);引发此异常:线程“AWT-EventQueue-0”java.lang.ArrayIndexOutOfBoundsException中的异常:数组索引超出范围: