Java容器删除方法无法正常工作 [英] Java Container remove method not working correctly

查看:159
本文介绍了Java容器删除方法无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我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);

但当我选择第一个radiobutton时,由于k = i + 2,应删除第一个JButton;
而不是删除这个删除TextArea(第一个)。
当我选择第3个radiobutton然后第1个JButton被删除。任何人都可以让我知道问题是什么?还有 System.out.println(i); System.out.println(k); 没有打印任何值....这里是代码

 公共类RadioDemo实现ActionListener {

String buttonName;
JPanel radioPanel = new JPanel();
ButtonGroup group = new ButtonGroup();
枚举enl;
int result;
ActionEvent e;
JRadioButton birdButton [];
int i,k;

Vector< String>列表名称;
Vector< JComponent>列表;
集装箱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(新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();
}

/ **收听单选按钮。 * /
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();
}
}

}


解决方案

getContentPane()返回的容器默认为 contentPane 由。


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

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);

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 System.out.println(i); System.out.println(k); is not printing any value....here is the code

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();
        }
    }

}

解决方案

The 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.

Instead, add on your own JComponent to the frame and operate on it; JPanel is a common choice.

Addendum: Also consider an alternative layout such as CardLayout, illustrated here.

这篇关于Java容器删除方法无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆