一个容器中放置一个按钮,我点击这个按钮,把这个按钮移除,该怎么做啊?import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class aaa extends JFrame implements ActionListener{
public aaa(){
setSize(800, 600);
setLocationRelativeTo(null);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton b=new JButton("dfdfdfdf");
add(b,BorderLayout.NORTH);
b.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
removeAll();
} public static void main(String[] args) throws Exception {   
new aaa();
}
}这是我写的一个示例代码,点击按钮以后,就没反应了,貌似是死了。

解决方案 »

  1.   

    public class aaa extends JFrame implements ActionListener
    {
     JButton b;
      public aaa(){
            setSize(800, 600);
            setLocationRelativeTo(null);
            setVisible(true);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            b=new JButton("dfdfdfdf");
            add(b,BorderLayout.NORTH);
            b.addActionListener(this);
        }
        public void actionPerformed(ActionEvent e) {
             this.remove(b);
             this.repaint();
        }     public static void main(String[] args) throws Exception {   
            new aaa();
        }}