[英]Removing JTextFields after dynamically adding it
My goal is to delete two JTextFields at the same time by clicking a JLabel. 我的目标是通过单击JLabel同时删除两个JTextField。
I've created the textfields like this: 我创建了这样的文本字段:
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();
}
});
And this is my code for removing the textfields: 这是我删除文本字段的代码:
public void mouseClicked(MouseEvent e) {
int countPlace= pnlPanel.getComponentCount();
int countOfRemaining =countPlace;
pnlPanel.remove(--countOfRemaining);
frmFrame.revalidate();
pnlPanel.remove(--countOfRemaining);
frmFrame.revalidate();
}
}); });
Instead of deleting the txtfields on the same row, it deletes it one by one, i dont want that. 与其删除同一行上的txtfields,不如一一删除它,我不希望这样。 Please help me.
请帮我。 Thank you.
谢谢。
Call pnlPanel.revalidate()
before repaint()
在
repaint()
之前调用pnlPanel.revalidate()
repaint()
And don't use setBounds(). 并且不要使用setBounds()。 Define porper
LayoutManager
instead. 而是定义porper
LayoutManager
。
Instead of this, 代替这个
public void mouseClicked(MouseEvent e) {
int countPlace= pnlPanel.getComponentCount();
int countOfRemaining =countPlace;
pnlPanel.remove(countOfRemaining-1);
frmFrame.repaint();
pnlPanel.remove(countOfRemaining-1);
frmFrame.repaint();
}
});
Use this 用这个
public void mouseClicked(MouseEvent e) {
int countPlace= pnlPanel.getComponentCount();
int countOfRemaining =countPlace;
pnlPanel.remove(--countOfRemaining);
frmFrame.revalidate();
pnlPanel.remove(--countOfRemaining);
frmFrame.revalidate();
}
});
The above one throws an ArrayIndeOutOfBounds Exception because the count remaining variable is not being decremented after you remove a component. 上面的一个抛出ArrayIndeOutOfBounds异常,因为删除组件后剩余计数变量未递减。 Hence the index goes out of bound when you try to remove the second time.
因此,当您尝试第二次删除时,索引超出范围。
I recommend that you add all of your JPanels
to a Map (I use HashMap) in the form of <Integer, JPanel>
. 我建议您以
<Integer, JPanel>
的形式将所有JPanels
添加到Map(我使用HashMap)。 Name them all in order, and then just do Map.remove(Map.size() - 1)
and Map.remove(Map.size() - 2)
. 依次命名它们,然后执行
Map.remove(Map.size() - 1)
和Map.remove(Map.size() - 2)
。
You can also get the current set of Integers (the keys) by doing a Map.keySet()
; 您还可以通过执行
Map.keySet()
获得当前的整数集(键Map.keySet()
;
Map<Integer, JPanel> temp = new HashMap<Integer, JPanel>();
temp.put(0, new JPanel());
temp.put(1, new JPanel());
temp.put(2, new JPanel());
temp.remove(temp.size() - 1);
Makes it much easier to maintain numerous sets of panels. 使维护大量面板更加容易。 In my applications I don't control them numerically, but with short names instead.
在我的应用程序中,我没有数字控制它们,而是使用短名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.