//Computer Science Example
//Using TextFields, Buttons and Frames

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

/* FrameDemo creates a frame, adds a Panel to it containing a TextArea and two Buttons.  The second button opens a second frame. */
public class FrameDemo extends Frame
{
     private Panel panel;
     private TextArea area;
     private Button show, second;
     private SecondFrame secondFrame;
 
     public FrameDemo ()
     {
          super ("Frame Demo");
          setSize (300, 300);

          // Add a window closer to the frame.
          addWindowListener (new WindowAdapter()
               {public void windowClosing(WindowEvent e) {System.exit(0);}});

          panel = new Panel (); // Get a new panel and set its background color.
          panel.setBackground (Color.yellow);
          area = new TextArea (10, 30); // Get the Text Area and add it to the panel.
          panel.add (area);
          // Get a new button and add a listener to it.
          show = new Button ("Show Data");
          show.addActionListener (new ShowListener ());
          panel.add (show);
          add (panel); // Add the panel to the frame.
 
          secondFrame = new SecondFrame ();
          second = new Button ("Second");  // Get a new button and add a listener to it.
          second.addActionListener (new ActionListener ()
               {public void actionPerformed (ActionEvent e){secondFrame.setVisible (true);}});
          panel.add (second);
     } // constructor

     // Inner class to listen for the show button.
     class ShowListener implements ActionListener
     {
          public void actionPerformed (ActionEvent event)
          {
               area.append ("This is a TextArea.\n");
          } // method actionPerformed
     } // class ShowListener
 
     // The main method gets a new frame and makes it visible.
     public static void main (String [] args)
     {
          FrameDemo frame = new FrameDemo ();
          frame.setVisible (true);
     }
} // class FrameDemo

// SecondFrame creates a second frame and adds a Panel, a TextArea and a Button to it.
class SecondFrame extends Frame
{
     private Panel panel;
     private TextArea area;
     private Button show, hide;
 
     public SecondFrame ()
     {
          super ("Second Frame");
          setSize (300, 300);

          // Get a new panel and set its background color.
          panel = new Panel ();
          panel.setBackground (Color.cyan);
          // Get the Text Area and add it to the panel.
          area = new TextArea (10, 30);
          panel.add (area);
          // Get a new button and add a listener to it.
          show = new Button ("Show Data");
          show.addActionListener (new ShowListener ());
          panel.add (show);
 
          hide = new Button ("Return");
          // Get a new button and add a listener to it.
          hide.addActionListener (new ActionListener ()
               {public void actionPerformed (ActionEvent e){setVisible (false);}});
          panel.add (hide);
          // Add the panel to the frame.
          add (panel);
     } // constructor

     // Inner class to listen for the show button.
     class ShowListener implements ActionListener
     {
          public void actionPerformed (ActionEvent event)
          {
               area.append ("This is some data.\n");
          } // method actionPerformed
     } // class HideListener
} // class SecondFrame