// 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 Dynamic extends JFrame implements ActionListener {
    private JPanel l;
    private JPanel r;
    private JButton button;
    private int n;

    public Dynamic() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().setLayout(new GridLayout(1, 2));
        l = new JPanel();
        r = new JPanel();
        button = new JButton("Switch");
        button.addActionListener(this);
        l.add(button);
        getContentPane().add(l);
        getContentPane().add(r);
        n = 0;
        pack();
    }
    public void actionPerformed(ActionEvent ev) {
        if (ev.getSource() == button) {
            if(n % 2 == 0) {
                r.add(new JLabel("It works"));
                pack();
                repaint();
            } else {
                r.removeAll();
                pack();
                repaint();
            }
            n++;
        }
    }
    public static void main(String[] args) {
        Dynamic f = new Dynamic();
        f.setVisible(true);
    }
}
