我HAVA添加1.TextArea 2.TextField然后我开始先后在容器中加入一个JButton ...,现在通过使用一个JRadioButton我想使用此代码从容器中取出的JButton
i=0;
k=0;
while(!birdButton[i].isSelected()){
i++;
}
System.out.println(i);
k=i+2;
list.removeElementAt(i);
listName.removeElementAt(i);
System.out.println(k);
c.getContentPane().remove(k);
但是当我选择第一单选按钮第一JButton的应因为k的被删除= I + 2; 而不是删除这一项会删除文本区(第一个)。 当我选择了第三个单选按钮,然后第一个的JButton被删除。 任何人都可以让我知道问题是什么? 并且还System.out.println(i);
System.out.println(k);
没有打印任何价值....这里是代码
public class RadioDemo implements ActionListener {
String buttonName;
JPanel radioPanel = new JPanel();
ButtonGroup group = new ButtonGroup();
Enumeration enl;
int result;
ActionEvent e;
JRadioButton birdButton[];
int i, k;
Vector<String> listName;
Vector<JComponent> list;
Container c;
public RadioDemo(Vector<String> listName,Vector<JComponent> list,Container c) {
birdButton=new JRadioButton[listName.size()];
this.listName=listName;
this.c=c;
this.list=list;
i = 0;
for (String buttonName : listName){
birdButton[i] = new JRadioButton(buttonName);
birdButton[i].setActionCommand(buttonName);
group.add(birdButton[i]);
birdButton[i].addActionListener(this);
radioPanel.add(birdButton[i]);
i++;
}
birdButton[0].setSelected(true);
radioPanel.setLayout(new BoxLayout
(radioPanel,BoxLayout.Y_AXIS));
//birdButton.setBorder
(BorderFactory.createEmptyBorder(5,5,5,5));
result = JOptionPane.showConfirmDialog(null, radioPanel, "Please choose", JOptionPane.OK_CANCEL_OPTION);
show();
}
/** Listens to the radio buttons. */
public void actionPerformed(ActionEvent e) {
this.e = e;
}
public void show() {
if (result == JOptionPane.OK_OPTION) {
i = 0;
k = 0;
while (!birdButton[i].isSelected()) {
i++;
}
System.out.println(i);
k = i + 2;
list.removeElementAt(i);
listName.removeElementAt(i);
System.out.println(k);
c.getContentPane().remove(k);
c.getContentPane().validate();
// System.out.println(e.getActionCommand());
// c.getContentPane().rePaint();
}
}
}