[ Back | Previous | Next ]

Using innerclasses - excellent for GUI purposes

Package:
java.awt.*
Product:
JDK
Release:
1.1.x
Related Links:
Color
Cursor
FileDialog
Font
FontMetrics
Frame
general
Image
LayoutManager
Menu
ScrollPane
Comment:
Using innerclasses - excellent for GUI purposes 

Briefly explain the source. The GUI is an innerclass of TerApp, an application. Because the GUI is strongly
connected with the TerApp it is nice to have this innerclass to organize GUI stuff from functional stuff of the
application. All the public or friendly variables from the outterclass are known in the innerclass. The same story
counts for the methods. By means of adapters and listeners we control the AWT environment. Further examples
see action adapter and windows adapter. 

       Outter class
                   ...
       Inner class
                   ...

For example: 

  
      public class TerApp {  
              private GUI gui;
              private TerBase tb  = null;
              private ViewBase vb;

          /** default constructor */
              public TerApp( ) {   
                      gui = this. new GUI(); // make a new GUI enclosed in the PacManApp
                      gui.setTitle( "Territory Administrator" ); // title of frame
                      gui.setSize( 600, 400);
                      gui.setVisible( true );  
              }  

              /** quit application */
              public void quitApp() {
                      gui.setVisible( false );
                      gui.dispose(); 
                      System.exit(0);
              }

              public static void main( String [] args ) {
                      TerApp tg = new TerApp(); 
              }

              // ENCLOSED GUI  
                      /** The GUI that controls the Territory Database */
              class GUI extends Frame {
                      // Menu
                      private MenuBar mb;
                      private Menu mfile;
                      private MenuItem  iexit;
                      // Panel South
                      private Panel statuspanel;   
          
          
              public GUI() {      
                  setBackground( Color.white );
                      setLayout( new BorderLayout() );   
                              ....                            
                              ....                            
                              ....                            
                              ....                            
             
                              // menu seperator
                      mfile.addSeparator();
                              // Exiting administrator
                      mfile.add( iexit = new MenuItem( "Exit" ) );
                              iexit.addActionListener(
                                              new ActionAdapter() {
                                                      public void actionPerformed(ActionEvent e) {  
                                                              System.out.println("Menu -> Options -> Text");
                                                              quitApp();
                                                      }
                                              } 
                              );
                      
                              ....                            
                              ....                            
                              ....                            
                              ....                            
                              ....                            

                       // set menubar
                      mb = new MenuBar();
                      mb.add( mfile );
                      setMenuBar( mb );
          
                              // Panel South layout
                      statuspanel = new Panel();
                      statuspanel.setLayout( new FlowLayout( FlowLayout.LEFT, 5, 5) ); 
          
          
                      // This.
                      add("South",statuspanel);
              }     
              } // ENCLOSED GUI CLASS  

      } // class
1