我的目標是通過單擊JLabel同時刪除兩個JTextFields。動態添加JTextFields後刪除JTextFields
我創建的文本框是這樣的:
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,它會逐一刪除它,我不希望這樣。請幫幫我。謝謝。
;嗨。我添加了重新驗證。我不明白的是爲什麼這個代碼:int countPlace = \t pnlTxtTxt.getComponentCount(); \t \t \t \t int countOfRemaining = countPlace; \t \t \t \t pnlTxtTxt.remove(countOfRemaining); \t \t \t \t frmGM.revalidate(); \t \t \t \t frmGM.repaint(); \t \t \t \t \t \t \t pnlTxtTxt.remove(countOfRemaining-1);引發此異常:線程「AWT-EventQueue-0」中的異常java.lang.ArrayIndexOutOfBoundsException:數組索引超出範圍:8 \t \t \t \t frmGM。重新驗證(); \t \t \t \t frmGM.repaint() – harraypotter