GUIGreet2.java

Download GUIGreet2.java

 1: // A stand-alone GUI Java program to displays several messages.
 2: // This program illustrates the creation of many objects,
 3: // private instance variables, and static data (class variables).
 4: // Also added code to close the application when the user clicks
 5: // the mouse in the close box, and other GUI window stuff.
 6: 
 7: // Written by Wayne Pollock, Tampa, FL USA, 1999
 8: 
 9: import java.awt.*;
10: import java.awt.event.*;
11: 
12: public class GUIGreet2 extends Frame
13: {
14:    private String message;
15:    private static int windowCount = 0;
16:    private static int position = 0;
17: 
18:    public GUIGreet2 ( String msg )
19:    {
20:       message = msg;
21:       ++windowCount;   ++position;
22:       setTitle( "A Friendly Greeting (" + windowCount + ")" );
23:       setSize( 300, 200 );
24:       setBackground( Color.cyan );
25:       setResizable( false );
26:       setLocation( position*50, position*50 );
27: 
28:       addWindowListener( new WindowAdapter()
29:          {  public void windowClosing( WindowEvent e )
30:             {  e.getWindow().dispose();
31:                if ( --windowCount == 0 )   // Exit program when the
32:                   System.exit( 0 );        // last window is closed.
33:             }
34:          }
35:       );
36: 
37:       setVisible( true );
38:    }
39: 
40:    public static void main ( String[] args )
41:    {
42:       GUIGreet2 one   = new GUIGreet2( "Hello, World!" );
43:       GUIGreet2 two   = new GUIGreet2( "Howdy, Class!" );
44:       GUIGreet2 three = new GUIGreet2( "Hey, Students!" );
45:    }
46: 
47:    public void paint ( Graphics g )
48:    {
49:       g.setColor( Color.red );
50:       g.drawRect( 30, 40, 240, 130 );
51:       g.setColor( Color.blue );
52:       g.setFont( new Font("SansSerif", Font.BOLD, 24) );
53:       g.drawString( message, 70, 110 );  // Position determined
54:    }                                     // by trial and error.
55: }