Source of HelloGUIApp5.java


  1: //HelloGUIApp5.java
  2: //The new feature to look at in this example is the
  3: //"anonymous inner class" object that replaces the
  4: //implementation of the WindowListener interface of
  5: //the previous example.

  7: import java.awt.*;
  8: import java.awt.event.*;

 10: public class HelloGUIApp5
 11: extends Frame
 12: implements ActionListener//, WindowListener <-Note
 13: {
 14:     private Button button;
 15:     private Label label;

 17:     public static void main(String[] args)
 18:     {
 19:         HelloGUIApp5 application = new HelloGUIApp5();
 20:         application.setSize(100, 100);
 21:         application.setVisible(true);
 22:     }


 25:     public HelloGUIApp5()
 26:     {
 27:         setTitle("Hello with Quit Button");

 29:         setLayout(new FlowLayout());

 31:         button = new Button("Quit");
 32:         button.setBackground(Color.yellow);
 33:         add(button);
 34:         button.addActionListener(this);

 36:         label = new Label("Hello, world!");
 37:         add(label);

 39:         //addWindowListener(this); <-Note
 40:         addWindowListener(new WindowAdapter()
 41:                           {
 42:                               public void windowClosing(WindowEvent e)
 43:                               {
 44:                                   System.exit(0);
 45:                               }
 46:                           });
 47:     }


 50:     public void actionPerformed(ActionEvent event)
 51:     {
 52:             System.exit(0);
 53:     }
 54: 
 55:     //Note:
 56:     //public void windowClosed(WindowEvent event) {}
 57:     //public void windowDeiconified(WindowEvent event) {}
 58:     //public void windowIconified(WindowEvent event) {}
 59:     //public void windowActivated(WindowEvent event) {}
 60:     //public void windowDeactivated(WindowEvent event) {}
 61:     //public void windowOpened(WindowEvent event) {}
 62:     //public void windowClosing(WindowEvent event)
 63:     //{
 64:     //        System.exit(0);
 65:     //}


 68:     //Try commenting out this method.
 69:     public void paint(Graphics g)
 70:     {
 71:         g.drawString("Hello, world!", 20, 90);
 72:     }
 73: }