package view; import javax.swing.*; // to use JFrame. /** * Minimal reusable code to create a JFrame with a title and a window event * listener to call on System.exit() and force termination of the main * application that creates this JFrame. * @author D.X. Nguyen */ public abstract class AFrame extends JFrame { /** * Constructor for an abstract frame. * Calls the superclass's constructor, adds a WindowListener to close the window, and then calls the abstract * initialize() method. * @param title window title */ public AFrame(String title) { // Always call the superclass's constuctor: super(title); // Add an anonymous WindowAdapter event handler to call System.exit to // force termination of the main application when the Frame closes. // Without it, the main application will still be running even after // the frame is closed. // For illustration purpose, we use the full package name for // WindowAdpater and WindowEvent. // We could have imported java.awt.event.* and avoid using the full // package names. addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent e) { System.exit(0); } }); // Subclasses are to do whatever is necessary to intialize the frame. initialize(); } /** * Relegates to subclasses the responsibilty to initialize the systemt to * a well-defined state. */ protected abstract void initialize(); }