The Java Program: Quit.java
1 // Quit.java -- application with just a quit button
2
3 import java.awt.Button;
4 import java.awt.Frame;
5 import java.awt.event.ActionEvent;
6 import java.awt.event.ActionListener;
7 import java.awt.event.WindowAdapter;
8 import java.awt.event.WindowEvent;
9
10 public class Quit extends Frame {
11
12 public static void main (String[] args) {
13 new Quit();
14 }
15
16 public Quit() {
17 setTitle ("Quit Button");
18 // default layout manager for "Frame" is "BorderLayout"
19
20 Button q = new Button ("Quit");
21
22 class QuitButtonListener implements ActionListener {
23 public void actionPerformed (ActionEvent e) { close(); }
24 }
25 q.addActionListener (new QuitButtonListener ());
26 add ("Center", q);
27
28 class WindowClosingListener extends WindowAdapter {
29 public void windowClosing (WindowEvent evt) { close(); }
30 }
31 addWindowListener (new WindowClosingListener ());
32 setSize (150, 100);
33 setVisible (true);
34 }
35
36 void close () {
37 setVisible (false);
38 dispose();
39 System.exit(0);
40 }
41 }