Java容器移除方法无法正常工作
我添加了1.TextArea 2.TextField 然后我开始在容器上依次添加JButton…,现在通过使用JRadioButton,我想使用以下代码从容器中删除JButtonJava容器移除方法无法正常工作,java,swing,jframe,jpanel,Java,Swing,Jframe,Jpanel,我添加了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.p
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;
它没有删除这个,而是删除了TextArea(第一个)。
当我选择第三个单选按钮时,第一个按钮被删除。有人能告诉我问题出在哪里吗?以及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();
}
}
}
公共类RadioDemo实现ActionListener{
字符串按钮名;
JPanel radioPanel=新的JPanel();
ButtonGroup=新建ButtonGroup();
枚举enl;
int结果;
行动事件e;
JRadioButton birdButton[];
int i,k;
向量列表名;
向量表;
容器c;
公共RadioDemo(向量列表名、向量列表、容器c){
birdButton=newjradiobutton[listName.size()];
this.listName=listName;
这个.c=c;
this.list=list;
i=0;
for(字符串按钮名称:listName){
birdButton[i]=新的JRadioButton(按钮名);
birdButton[i].setActionCommand(buttonName);
添加组(birdButton[i]);
birdButton[i].addActionListener(此);
radioPanel.add(鸟按钮[i]);
i++;
}
birdButton[0].setSelected(true);
radioPanel.setLayout(新的BoxLayout
(无线面板,方框布局,Y_轴);
//鸟钮扣
(BorderFactory.createEmptyByOrder(5,5,5,5));
结果=JOptionPane.showConfirmDialog(空,radioPanel,“请选择”,JOptionPane.OK\u CANCEL\u选项);
show();
}
/**听单选按钮*/
已执行的公共无效操作(操作事件e){
这个。e=e;
}
公开展览({
if(result==JOptionPane.OK\u选项){
i=0;
k=0;
而(!birdButton[i].isSelected()){
i++;
}
系统输出打印LN(i);
k=i+2;
清单1.移除元素(i);
listName.removeElementAt(i);
系统输出println(k);
c、 getContentPane()。删除(k);
c、 getContentPane().validate();
//System.out.println(e.getActionCommand());
//c.getContentPane().rePaint();
}
}
}
由getContentPane()
返回的容器
默认情况下是由JFrame
管理的JRootPane的contentPane
。尽管“为了方便起见,add
方法及其变体、remove
和setLayout
已被覆盖,以便根据需要转发到contentPane
”,但没有先验的方法来了解框架内部使用的组件索引
相反,在框架中添加自己的JComponent
,并对其进行操作JPanel
是常见的选择
附录:也考虑一个替代的布局,如<代码>卡布局>代码> .t/p>如何,在你要删除的单选按钮之间保持一个关系,也许类似于<代码> HashMap < /代码>?我需要看看你是如何构造UI的。well@kleopatra:绝对正确;感谢您的批评性评论;以上更多。只是阅读:-)很酷的编辑,很乐意给你第二次投票!