Download this source file


// A stand-alone GUI Java program to displays several messages.
// This program illustrates the creation of many objects,
// private instance variables, and static data (class variables).
// Also added code to close the application when the user clicks
// the mouse in the close box, and other GUI window stuff.

// Written by Wayne Pollock, Tampa, FL USA, 1999

import java.awt.*;
import java.awt.event.*;

public class GUIGreet2 extends Frame
{
   private String message;
   private static int windowCount = 0;
   private static int position = 0;

   public GUIGreet2 ( String msg )
   {
      message = msg;
      ++windowCount;   ++position;
      setTitle( "A Friendly Greeting (" + windowCount + ")" );
      setSize( 300, 200 );
      setBackground( Color.cyan );
      setResizable( false );
      setLocation( position*50, position*50 );

      addWindowListener( new WindowAdapter()
         {  public void windowClosing( WindowEvent e )
            {  e.getWindow().dispose();
               if ( --windowCount == 0 )   // Exit program when the
                  System.exit( 0 );        // last window is closed.
            }
         }
      );

      setVisible( true );
   }

   public static void main ( String[] args )
   {
      GUIGreet2 one   = new GUIGreet2( "Hello, World!" );
      GUIGreet2 two   = new GUIGreet2( "Howdy, Class!" );
      GUIGreet2 three = new GUIGreet2( "Hey, Students!" );
   }

   public void paint ( Graphics g )
   {
      g.setColor( Color.red );
      g.drawRect( 30, 40, 240, 130 );
      g.setColor( Color.blue );
      g.setFont( new Font("SansSerif", Font.BOLD, 24) );
      g.drawString( message, 70, 110 );  // Position determined
   }                                     // by trial and error.
}