// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ButtonFun extends JFrame implements ActionListener {
    private JButton add;
    private int n;
    public ButtonFun() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().setLayout(new GridLayout(10, 10));
        add = new JButton("Add new");
        add.addActionListener(this);
        getContentPane().add(add);
        pack();
        n = 0;
    }
    public void actionPerformed(ActionEvent ev) {
        if (ev.getSource() == add) {
            n++;
            JButton del = new JButton("Delete me (" + n + ")");
            del.addActionListener(this);
            getContentPane().add(del);
        } else {
            JButton del = (JButton)ev.getSource();
            del.getParent().remove(del);
        }
        pack();
        repaint();
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame f = new ButtonFun();
                f.setVisible(true);
            }
        });
    }
}
