SDSU CS 596 Java Programming
Fall Semester, 1998
Window Applet
To Applet Index
San Diego State University -- This page last updated 16-Dec-98

Description

This applet shows how to open a window

Applet

Your browser does not support Java applets, so you can not see this applet operate.

Applet Tag

<APPLET 
   ARCHIVE="AppletClasses.jar" 
   CODE="WindowApplet.class" 
   WIDTH="100" HEIGHT="50"
   ALIGN="right" >
   Your browser does not support Java applets, so
   you can not see this applet operate.
</APPLET>

Applet Source Code

ButtonWindow.java

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

// The applet will use this class to open a window.
public class  ButtonWindow  extends Frame 
  {
  Button red = new Button( "Red" );
  Button blue = new Button( "Blue" );
  
  public ButtonWindow( int  widthInPixels, int heightInPixels ) 
    {
    setTitle( "Button Example" );
    setSize( widthInPixels, heightInPixels );
    setLayout( new FlowLayout() );
    add( red );
    add( blue );
    
    red.addActionListener( new ColorActionListener( Color.red) );
    blue.addActionListener( new ColorActionListener( Color.blue) );
    }
  
  class ColorActionListener implements ActionListener 
    {
    Color backgroundColor;
    
    public ColorActionListener( Color aColor ) 
      {
      backgroundColor = aColor;
      }
    
    public void actionPerformed( ActionEvent event )  
      {
      setBackground( backgroundColor );
      repaint();    // Show effect of color change
      }
    }
  }

WindowApplet.java

import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;

public class  WindowApplet  extends Applet 
  {
  Button display = new Button( "Open" );
  Frame buttonWindow = new ButtonWindow( 75, 75 );
  public void init( ) 
    {
    add( display );
    
    display.addActionListener( new DisplayListener( ) );
    }
  
  class DisplayListener implements ActionListener 
    {
    public void actionPerformed( ActionEvent event )  
      {
      if ( buttonWindow.isVisible() ) 
        {
        display.setLabel( "Open" );
        buttonWindow.setVisible( false );
        } 
      else 
        {
        display.setLabel( "Close" );
        buttonWindow.setVisible( true );
        }
      }
    }
  }


Visitors since 16-Dec-98