我有兩個標籤,一個用於添加,一個用於刪除JtextFields。我可以添加jTextFields並將其刪除,但我的目標是刪除標籤單擊上的文本字段。如何在標籤上動態添加和刪除JTextField點擊
下圖顯示了我的目標,每次單擊「+」標籤時,它下面的文本框都會被創建。當我點擊「 - 」標籤時,同一行上的文本框將被刪除。 我該怎麼做才能做到這一點?
這是我的代碼:
lblRemoveTf = new JLabel();
lblRemoveTf.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
for(JTextField myTxt1 : myListOfTxtNum){
eto1 = myTxt1;
}
myListOfTxtNum.remove(eto1);
pnlTxtTxt.remove(pnlTxtTxt.getComponentAt(eto1.getLocation()));
frmGM.revalidate();
frmGM.repaint();
for(JTextField myTxt : myListOfTxtName){
eto = myTxt;
}
myListOfTxtName.remove(eto);
pnlTxtTxt.remove(pnlTxtTxt.getComponentAt(eto.getLocation()));
x-=50;
y-=50;
frmGM.revalidate();
frmGM.repaint();
}
});
lblRemoveTf.setBounds(450,6, 125, 25);
pnlTxtTxt.add(lblRemoveTf);
lblRemoveTf.setIcon(new ImageIcon(GroupManagement.class.getResource("/app/resources/minussmall.png")));
lblAddNewTF = new JLabel();
lblAddNewTF.setBounds(420, 6, 38, 25);
lblAddNewTF.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
count++;
txtStudentName= new JTextField();
txtStudentNumber = new JTextField();
myListOfTxtName.add(txtStudentName);
myListOfTxtNum.add(txtStudentNumber);
txtStudentName.setName("txtStudentname"+count);
txtStudentNumber.setName("txtStudentNumber" + count);
pnlTxtTxt.add(txtStudentName);
pnlTxtTxt.add(txtStudentNumber);
doContainTheListsOfTxt(txtStudentName, txtStudentNumber);
if(count>0){
x+=50;
y+=50;
txtStudentName.setBounds(225,6+y, 182, 27);
txtStudentNumber.setBounds(35, 6+y, 182, 27);
txtStudentName.setName(tempBox+count);
if(pnlTxtTxt.getComponentCount() >9){
pnlTxtTxt.setPreferredSize(new Dimension(450+y,50+y));
pnlTxtTxt.add(txtStudName);
pnlTxtTxt.add(txtStudentNumber);
frmGM.repaint();
scrpTxtTxt.revalidate();
}
}
frmGM.repaint();
}
});
lblAddNewTF.setIcon(new ImageIcon(GroupManagement.class.getResource("/app/resources/plussmall.png")));
pnlTxtTxt.add(lblAddNewTF);
,這是我想要實現
'+'和'-'是標籤嗎?爲什麼標籤必須像命令按鈕一樣工作?你真的很想要魔法;) – bonCodigo
是的。這是不可能的? – harraypotter
我在第一行做了它,我只是不知道如何在後續標籤上做到這一點。 – harraypotter