Java 容器删除方法无法正常工作
我添加了 1.TextArea 2.TextField然后我开始在容器上连续添加 JButton...,现在通过使用 JRadioButton 我想使用此代码从容器中删除 JButton
i=0;k=0;while(!birdButton[i].isSelected()){我++;}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(第一个).当我选择第三个单选按钮时,第一个 JButton 被删除.谁能让我知道问题是什么?还有 System.out.println(i);
System.out.println(k);
没有打印任何值....这里是代码
公共类 RadioDemo 实现 ActionListener {字符串按钮名称;JPanel radioPanel = new JPanel();ButtonGroup 组 = new ButtonGroup();枚举 enl;整数结果;动作事件 e;JRadioButton birdButton[];整数 i, k;向量<字符串>列表名称;向量<J分量>列表;容器 c;public RadioDemo(Vector listName,Vector list,Container c) {birdButton=新的 JRadioButton[listName.size()];this.listName=列表名;这个.c=c;这个.list=列表;我 = 0;对于(字符串按钮名称:列表名称){birdButton[i] = new JRadioButton(buttonName);birdButton[i].setActionCommand(buttonName);group.add(birdButton[i]);birdButton[i].addActionListener(this);radioPanel.add(birdButton[i]);我++;}birdButton[0].setSelected(true);radioPanel.setLayout(新的 BoxLayout(radioPanel,BoxLayout.Y_AXIS));//birdButton.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));结果 = JOptionPane.showConfirmDialog(null, radioPanel, "请选择", JOptionPane.OK_CANCEL_OPTION);展示();}/** 监听单选按钮.*/公共无效actionPerformed(ActionEvent e){这.e = e;}公共无效显示(){如果(结果 == JOptionPane.OK_OPTION){我 = 0;k = 0;while (!birdButton[i].isSelected()) {我++;}System.out.println(i);k = 我 + 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();}}}
解决方案 i hava added 1.TextArea 2.TextField
then i start adding JButton successively on container...,now by using JRadioButton i want to remove JButton from container using this code but when i select the 1st radiobutton 1st JButton should be deleted because of k=i+2;
instead of deleting this one it deletes the TextArea(1st one).
when i select the 3rd radiobutton then 1st JButton is deleted. can anyone let me know what the problem is? and also
The Instead, add on your own Addendum: Also consider an alternative layout such as getContentPane()
返回的Container
默认是contentPane
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);
System.out.println(i);
System.out.println(k);
is not printing any value....here is the codepublic 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();
}
}
}
Container
returned by getContentPane()
is, by default, the contentPane
of a JRootPane
managed by the top-level container, JFrame
. Although, "as a convenience, the add
method and its variants, remove
and setLayout
have been overridden to forward to the contentPane
as necessary," there's no a priori way to know about the frame's internal use of component indices.JComponent
to the frame and operate on it; JPanel
is a common choice.CardLayout
, illustrated here.
相关文章