Java Window Event (and Frame control) Demo

Download FrameTst.java

// Demo of Window controls and events.
//
// Written 5/2006 by Wayne Pollock, Tampa Florida USA.

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

class FrameTst extends Frame implements WindowListener
{
   private Frame frame;

   public static void main ( String [] args )
   {

      Frame control = new FrameTst( "Test Frame Control" );
      control.pack();
      control.setLocation( 250, 0 );
      control.addWindowListener( new WindowAdapter()
       { public void windowClosing ( WindowEvent we )
        { System.exit( 0 ); }
       }
      );
      control.setVisible( true );
   }

   public FrameTst ( String title )
   {
      super( title );

      // Create Components and set their properties:

      Button show1 = new Button( "setVisible(true)  " );
      Button show2 = new Button( "toFront()" );
      Button show3 = new Button( " setExtendedState(Frame.NORMAL)  " );

      Button hide1 = new Button( "setVisible(false)" );
      Button hide2 = new Button( "toBack()" );
      Button hide3 = new Button( "setExtendedState(Frame.ICONIFIED)" );

      Button create  = new Button( " create()  " );
      Button dispose = new Button( "dispose()" );

      // Add and layout components:

      Panel show = new Panel( new FlowLayout(FlowLayout.LEFT,5,5) );
      Panel hide = new Panel( new FlowLayout(FlowLayout.LEFT,5,5) );
      add( show, BorderLayout.NORTH );
      add( hide, BorderLayout.SOUTH );

      show.add( create );
      show.add( show1 );
      show.add( show2 );
      show.add( show3 );
      hide.add( dispose );
      hide.add( hide1 );
      hide.add( hide2 );
      hide.add( hide3 );

      // Hook up Event Handling:

      show1.addActionListener( new ActionListener()
        {  public void actionPerformed( ActionEvent e )
           {  frame.setVisible( true ); }
        }
      );

      show2.addActionListener( new ActionListener()
        {  public void actionPerformed( ActionEvent e )
           {  frame.toFront(); }
        }
      );

      show3.addActionListener( new ActionListener()
        {  public void actionPerformed( ActionEvent e )
           {  frame.setExtendedState( Frame.NORMAL ); }
        }
      );

      hide1.addActionListener( new ActionListener()
        {  public void actionPerformed( ActionEvent e )
           {  frame.setVisible( false ); }
        }
      );

      hide2.addActionListener( new ActionListener()
        {  public void actionPerformed( ActionEvent e )
           {  frame.toBack(); }
        }
      );

      hide3.addActionListener( new ActionListener()
        {  public void actionPerformed( ActionEvent e )
           {  frame.setExtendedState( Frame.ICONIFIED ); }
        }
      );

      create.addActionListener( new ActionListener()
        {  public void actionPerformed( ActionEvent e )
           {  frame = new Frame( "Test Frame" );
              frame.setBounds( 0, 0, 200, 100 );
              frame.addWindowListener( FrameTst.this );
           }
        }
      );

      dispose.addActionListener( new ActionListener()
        {  public void actionPerformed( ActionEvent e )
           {  frame.dispose();  frame = null; }
        }
      );
   }

      public void windowOpened ( WindowEvent we )
      {   System.out.println( "WindowOpened Event" );  }

      public void windowClosed ( WindowEvent we )
      {   System.out.println( "WindowClosed Event" );  }

      public void windowClosing ( WindowEvent we )
      {   System.out.println( "WindowClosing Event" );  }

      public void windowIconified ( WindowEvent we )
      {   System.out.println( "WindowIconified Event" );  }

      public void windowDeiconified ( WindowEvent we )
      {   System.out.println( "WindowDeiconified Event" );  }

      public void windowActivated ( WindowEvent we )
      {   System.out.println( "WindowActivated Event" );  }

      public void windowDeactivated ( WindowEvent we )
      {   System.out.println( "WindowDeactivated Event" );  }
}