SimpleGUI.java
(Six AWT and Two Swing Versions)

SimpleGUI1.java

Download SimpleGUI1.java (source)


// This program creates a blank window, and shows how to
// set the size and title.
// Written 1999 by Wayne Pollock, Tampa FL USA.

import java.awt.*;

public class SimpleGUI1
{   public static void main ( String [] args )
    {   Frame myFrame = new Frame();
        myFrame.setTitle( "Wonder Window!" );
        myFrame.setSize( 250, 100 );
        myFrame.setVisible( true );
    }
}

 

SimpleGUI2.java

Download SimpleGUI2.java (source)


// This program creates a blank window, and shows how to
// set the size and title.  It also shows the use of a
// WindowListener, so the window can be closed by clicking
// the close box.
// Written 1999 by Wayne Pollock, Tampa FL USA.

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

public class SimpleGUI2
{   public static void main ( String [] args )
    {   Frame myFrame = new Frame();
        myFrame.setTitle( "Wonder Window!" );
        myFrame.setSize( 250, 100 );
        myFrame.setVisible( true );
        MyWindowHandler mwh = new MyWindowHandler();
        myFrame.addWindowListener( mwh );
    }
}

class MyWindowHandler implements WindowListener
{
public void windowClosed      ( WindowEvent we ) { }
public void windowDeiconified ( WindowEvent we ) { }
public void windowIconified   ( WindowEvent we ) { }
public void windowActivated   ( WindowEvent we ) { }
public void windowDeactivated ( WindowEvent we ) { }
public void windowOpened      ( WindowEvent we ) { }
public void windowClosing     ( WindowEvent we ) { System.exit( 0 ); }
}

 

SimpleGUI3.java

Download SimpleGUI3.java (source)


// This program creates a blank window, and shows how to
// set the size and title.  This version shows the use of
// a WindowAdapter class.  Since this program doesn't extend
// from any other class, it could extend from WindowAdapter.  But
// applets and many other programs will already extend from some
// class or another, so a separate class is needed.
// Written 1999 by Wayne Pollock, Tampa FL USA.

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

public class SimpleGUI3
{   public static void main ( String [] args )
    {   Frame myFrame = new Frame();
        myFrame.setTitle( "Wonder Window!" );
        myFrame.setSize( 250, 100 );
        myFrame.setVisible( true );
        MyWindowHandler mwh = new MyWindowHandler();
        myFrame.addWindowListener( mwh );
    }
}

class MyWindowHandler extends WindowAdapter
{   public void windowClosing ( WindowEvent we )
    {   System.exit( 0 );
    }
}

 

SimpleGUI4.java

Download SimpleGUI4.java (source)


// This program creates a blank window, and shows how to
// set the size and title.  The class itself implements the
// WindowListener interface.  Since an object must be constructed,
// Most of the window details have been moved from "main" to the
// constructor.  (Note the use of an implied "this" in front of
// methods such as "setTitle".)
// Written 1999 by Wayne Pollock, Tampa FL USA.

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

public class SimpleGUI4 extends Frame implements WindowListener
{
   public static void main ( String [] args )
    {   Frame myFrame = new SimpleGUI4();
    }

    SimpleGUI4 ()
    {   setTitle( "Wonder Window!" );
        setSize( 250, 100 );
        setVisible( true );
        addWindowListener( this );
    }

    public void windowClosed      ( WindowEvent we ) { }
    public void windowDeiconified ( WindowEvent we ) { }
    public void windowIconified   ( WindowEvent we ) { }
    public void windowActivated   ( WindowEvent we ) { }
    public void windowDeactivated ( WindowEvent we ) { }
    public void windowOpened      ( WindowEvent we ) { }
    public void windowClosing     ( WindowEvent we ) { System.exit( 0 ); }
}

 

SimpleGUI5.java

Download SimpleGUI5.java (source)


// This program creates a blank window, and shows how to
// set the size and title.  In this version, an "anonymous"
// inner class is used.  (An inner class is one defined inside
// another class, an anonymous class is an un-named class.)  This
// is probably the most commonly used method for simple event
// handlers, although anonymous inner classes don't work too well
// in more complex cases.  (And are darned ugly too!)
// Written 1999 by Wayne Pollock, Tampa FL USA.

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

public class SimpleGUI5 extends Frame
{
   public static void main ( String [] args )
    {   Frame myFrame = new SimpleGUI5();
    }

    SimpleGUI5 ()
    {   setTitle( "Wonder Window!" );
        setSize( 250, 100 );
        setVisible( true );

        addWindowListener( new WindowAdapter()
            {   public void windowClosing ( WindowEvent we )
                {   System.exit( 0 );
                }
            }
        );
    }
}

 

SimpleGUI6.java

Download SimpleGUI6.java (source) 
Download WPIcon.gif


// This program creates a window, and shows how to add an
// Icon, set a Font, add a Label, and center the Frame on
// the screen.  (We do it all!)
// Written 2001 by Wayne Pollock, Tampa FL USA.

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

public class SimpleGUI6 extends Frame
{
   public static void main ( String [] args )
    {   Frame myFrame = new SimpleGUI6();

        myFrame.addWindowListener( new WindowAdapter()
            {   public void windowClosing ( WindowEvent we )
                {   System.exit( 0 );
                }
            }
        );
        // Shrink the Frame to be just big enough for the contents:
//        myFrame.pack();
        myFrame.setVisible( true );  // Frames can use toFront, toBack too.
    }

    SimpleGUI6 ()
    {   setTitle( "Wonder Window!" );
        setSize( 350, 200 );

        // The AWT toolkit has lots of useful methods:
        Toolkit tk = Toolkit.getDefaultToolkit();

        // Center the Frame on the screen:
        int screenWidth = tk.getScreenSize().width;
        int screenHeight = tk.getScreenSize().height;
        int frameWidth = getWidth();
        int frameHeight = getHeight();

        setLocation( ( screenWidth - frameWidth ) / 2,
                     ( screenHeight - frameHeight ) / 2 );

        // Add an Icon:
        Image icon = tk.getImage( "WPIcon.gif" );
        setIconImage( icon );

        // Set the Font:
        setFont( new Font( "Serif", Font.BOLD, 18 ) );

        // Add a Label:
        add( new Label( "Is this cool or what?", Label.CENTER ) );
    }
}

 

SimpleGUI3.java - swing version

Download SimpleGUI3Swing.java (source)


// This program creates a blank window, and shows how to
// set the size and title.  This version shows the swing JFrame
// and how to handle window closing.
// (C) 2003 by Wayne Pollock, Tampa FL USA.  All Rights Reserved.

import javax.swing.*;

public class SimpleGUI3Swing
{   public static void main ( String [] args )
    {   JFrame myFrame = new JFrame();
        myFrame.setTitle( "Wonder Window!" );
        myFrame.setSize( 250, 100 );
        myFrame.setVisible( true );
        myFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    }
}

 

SimpleGUI6.java - swing version

Download SimpleGUI6Swing.java (source) 
Download WPIcon.gif


// This swing GUI program creates a window, shows how to add an
// Icon, set a Font, add a Button (which changes color when clicked),
// and center the Frame on the screen.  (We do it all!)
// (C) 2003 by Wayne Pollock, Tampa FL USA.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SimpleGUI6Swing extends JFrame
{
   public static void main ( String [] args )
    {   JFrame myFrame = new SimpleGUI6Swing();

        myFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        // Shrink the Frame to be just big enough for the contents:
//        myFrame.pack();
        myFrame.setVisible( true );  // Frames can use toFront, toBack too.
    }

    SimpleGUI6Swing ()
    {   setTitle( "Wonder Window - swing style!" );
        setSize( 350, 200 );

        // The AWT toolkit has lots of useful methods:
        Toolkit tk = Toolkit.getDefaultToolkit();

        // Center the Frame on the screen:
        int screenWidth = tk.getScreenSize().width;
        int screenHeight = tk.getScreenSize().height;
        int frameWidth = getWidth();
        int frameHeight = getHeight();

        setLocation( ( screenWidth - frameWidth ) / 2,
                     ( screenHeight - frameHeight ) / 2 );

        // Add an Icon:
        Image icon = tk.getImage( "WPIcon.gif" );
        setIconImage( icon );

        // Add a button:
        final JButton b = new JButton( "Change the color" );

        // Set the button's Font:
        b.setFont( new Font( "Serif", Font.BOLD, 18 ) );

        // Add button to JFrame's content pane directly:
        //Container c = getContentPane();
        //c.add( b );
        add( b );  // As of JAVA 1.4 this adds to the content pane
        
        // Add event listener for button:
        b.addActionListener( new ActionListener()
           {
              public void actionPerformed( ActionEvent e )
              {
                 b.setBackground( Color.BLUE );
              }
           }
        );
    }
}