Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
511 views
in Technique[技术] by (71.8m points)

swing - Java Container remove method not working correctly

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

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

image

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...