Source of InnerClassDemo.java


  1: 
  2: import javax.swing.JFrame;
  3: import javax.swing.JLabel;
  4: import java.awt.BorderLayout;
  5: import java.awt.Container;
  6: import java.awt.event.WindowAdapter;
  7: import java.awt.event.WindowEvent;
  8: 
  9: public class InnerClassDemo extends JFrame
 10: {
 11:     public static final int WIDTH = 300; 
 12:     public static final int HEIGHT = 200;
 13: 
 14:     /**
 15:      Creates and displays a window of the class InnerClassDemo.
 16:     */
 17:     public static void main(String[] args)
 18:     {
 19:         InnerClassDemo sampleGUI = new InnerClassDemo( );
 20:         sampleGUI.setVisible(true);
 21:     }
 22:     
 23:     public InnerClassDemo( )
 24:     {
 25:         setSize(WIDTH, HEIGHT);
 26:         setTitle("Inner Class Demo");
 27:         Container contentPane = getContentPane( );
 28:         contentPane.setLayout(new BorderLayout( ));
 29: 
 30:         JLabel label = new JLabel(
 31:                           "Please don't click that button!");
 32:         contentPane.add(label, BorderLayout.CENTER);
 33: 
 34:         addWindowListener(new InnerDestroyer( ));
 35:     }
 36: 
 37:     //An inner class with the same functionality
 38:     //as the class WindowDestroyer.
 39:     private class InnerDestroyer extends WindowAdapter
 40:     {
 41:         public void windowClosing(WindowEvent e) 
 42:         {
 43:             System.exit(0);
 44:         }
 45:     }
 46: }