September 19th, 2022

OOP for GUI

A button can be its own listener, by both extending JButton and implementing the ActionListener interface.

import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Button extends JButton implements ActionListener {
    public Button (String label) {
			setText (label);
			addActionListener (this);
    }

    public void actionPerformed (ActionEvent e) {
			System.out.println ("Button was pushed");
    }
}