单击后删除按钮?

run*_*nit 1 java action jframe jbutton

我如何让我的程序工作,以便在单击按钮时将其删除?

这是代码:

//Mainmenu
JFrame frame1 = new JFrame();
Container pane = frame1.getContentPane();

JButton a = new JButton(new ImageIcon("path2img"));
BufferedImage a1 = ImageIO.read(new File("path2img"));

 public Menu() throws IOException {
     frame1.setSize(300, 450);
    frame1.setLocationRelativeTo(null);
    frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame1.setResizable(false);
    frame1.setVisible(true);
    pane.add(a);
    a.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent aa) {
            pane.remove(a);

        }


    });
            }
Run Code Online (Sandbox Code Playgroud)

谢谢

Con*_*Del 5

任何时候向屏幕上已显示的内容添加或删除组件时,都必须调用 (re)validate(); repaint();