我创建了一个button对象,并设置了选择监听接口,事件响应的效果是将这个按钮本身删除
可以用什么方法实现?
谢谢

解决方案 »

  1.   

    你说的删除应该是指将它从容器中移除吧。你可以调用remove方法。
      

  2.   

    容器方法中倒是有不少remove方法,不过都是一些移除监听接口的函数,没有删除按钮控件的啊
      

  3.   


    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     *//**
     *
     * @author Administrator
     */
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    public class NewClass {
        JFrame frame;
        Container contentPane;
        JButton bun[];
        public NewClass(){
            frame=new JFrame();
            frame.setSize(400,400);
            contentPane=frame.getContentPane();
            contentPane.setLayout(new FlowLayout());
            bun=new JButton[10];
            for(int i=0;i<10;i++){
                bun[i]=new JButton(""+i);
                bun[i].addActionListener(new ActionListener(){
                    public void actionPerformed(ActionEvent e){
                        JButton b=(JButton)e.getSource();
                        //删除组件
                        contentPane.remove(b);
                        contentPane.validate();
                    }
                });
                contentPane.add(bun[i]);
            }
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
        public static void main(String args[]){
            new NewClass();
        }
    }
      

  4.   

    从 Container 继承的
    public void remove(Component c) 
              Removes the specified component from this container.
      

  5.   

    调用 revalidate 而不是 validate
      

  6.   

    忘了说了,我用的是SWT
    要删除的button我调用了dispose方法,能实现按钮不见,同时我在监听函数中还调用了另一个函数,功能是在另外一块区域布局按钮,但是函数的功能总是没实现,是不是button已经dispose了,监听函数的生命期结束了,其中包含的函数也得不到执行?