Anonymous classes can be declared to extend another class or to implement a single interface.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
public class MainClass {
public static void main(String[] argv) {
JButton theButton = new JButton();
theButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("The action has occurred");
}
});
}
}
|