import java.awt.*; import java.awt.event.*; /* CloseableFrame. B. Sanders, Oct. 97 CloseableFrame extends Frame to provide default size and location based on screen size, and to handle windowClosing events by exiting the program. The method IOLoopRepaint should be used instead of repaint when called within an IOloop reading from the console. It lowers the priority of the IO loop thread so that the frame can be repainted. */ public class CIS3020CloseableFrame extends Frame { public CIS3020CloseableFrame() { super(); addWindowListener(new FrameEvents()); Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); setSize(d.width/2, d.height/2); setLocation(d.width/4,d.height/4); } public CIS3020CloseableFrame(String title) { super(title); addWindowListener(new FrameEvents()); Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); setSize(d.width/2, d.height/2); setLocation(d.width/4,d.height/4); } public static void main(String[] args) { CIS3020CloseableFrame f = new CIS3020CloseableFrame("Closeable Frame"); f.setVisible(true); } public void IOLoopRepaint() { repaint(); Thread cur = Thread.currentThread(); int priority = cur.getPriority(); cur.setPriority(Thread.MIN_PRIORITY); cur.setPriority(priority); } /* For handling windowClosing events. Note that * this class is an inner class defined inside CIS3020CloseableFrame class. */ class FrameEvents extends WindowAdapter { public void windowClosing(WindowEvent event) { System.exit(0); } } }