Παράδειγμα προγραμματισμού με συμβάντα

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JOptionPane;

import java.awt.BorderLayout;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class EventDemo {
    class AlertAction implements ActionListener {
        private JFrame parent;

        AlertAction(JFrame parent) {
            this.parent = parent;
        }

        @Override public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(parent, "information", "Button Pressed!!", JOptionPane.INFORMATION_MESSAGE);
        }
    }

    public EventDemo() {
        JFrame jf = new JFrame("Hello, World!");
        JButton jb = new JButton("Click Me!");

        jf.setBounds(0, 0, 800, 600);

        jf.setLayout(new BorderLayout());
        jf.add(jb, BorderLayout.CENTER);

        jb.addActionListener(new AlertAction(jf));

        // Remember, the method show() is deprecated
        jf.setVisible(true);
    }

    public static void main(String[] args) {
        new EventDemo();
    }
}