Please send questions to st10@humboldt.edu .
/**
 * our second example of a Java application using a Frame;
 * now we have some sensitive components. 
 * Note that we are now including multiple top-level classes
 * within this file (only 1 is public, however), and we are
 * using more inner classes (for additional event handling).
 *
 *
 * by: Sharon M. Tuttle
 * last modified: 2-5-01
**/

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

// question: how come no "implements" clause? Because inner class is going to,
// instead;
public class GuiApplic2
{
	public static void main(String args[])
	{
		// again, our application's main() method is setting up
		// a frame --- but this time, it is an instance of
		// a subclass of Frame defined below in this file!
		GuiApplic2Frame mainFrame = new GuiApplic2Frame();
	
		// if you do not set the title for a Frame in its
		// constructor, you can still set it later using 
		// setTitle()
		mainFrame.setTitle("GuiApplic2");
		mainFrame.show();

		// I'll set up size and window-closing in class 
		// GuiApplic2Frame...
	}
}

// hey! this is LEGAL? having more than one top-level class in
// a file? Yup; BUT note that only ONE class at the top level
// within a file can be declared as public --- and THAT is the
// one the file is named after;
//
// note that it extends Frame... it is a subclass of Frame.
class GuiApplic2Frame extends Frame
{
	// these need to be visible to the inner classes AND to
	// constructor GuiApplic2Frame()
	private int 	count;
	private Label 	countLabel;
	private Button	toggleButton, countButton;
	private boolean nowGreen;

	// needs a constructor! We call it in main(), after all;
	public GuiApplic2Frame()
	{
		Label	welcome, author, version;

		// these will be instantiated as inner listener
		// classes for the two buttons
		ActionListener ctListener, toggListener;	
		
		// set up window-closing for the instance of this
		// GuiApplic2Frame, again with an anonymous inner class
		addWindowListener(new WindowAdapter()
			{
				public void windowClosing(WindowEvent e)
				{
					System.exit(0);
				}
			});

		// note: the default layout manager for a Frame is
		// BorderLayout! CHANGE it if you do not want that;
		setLayout(new FlowLayout());

		// these are simply labels, to start.
		welcome = new Label("Welcome to a 2nd Application with a GUI");
		welcome.setForeground(Color.blue);
		add(welcome);   

		author = new Label("Sharon M. Tuttle");
		author.setForeground(Color.black);
		add(author);

		version = new Label("Version 1.2");
		version.setForeground(Color.red);
		add(version);

		countButton = new Button("count clicks");
		add(countButton);
		count = 0;
		// instantiate ActionListener ctListener to be an
		// instance of inner class CountListener, add
		// ctListener as an action listener to countButton
		ctListener = new CountListener();
		countButton.addActionListener(ctListener);

		countLabel = new Label("count clicks button pressed: 0 times");
		add(countLabel);
		
		nowGreen = true;
		toggleButton = new Button("toggle color");
		toggleButton.setBackground(Color.green);
		add(toggleButton);
		// instantiate ActionListener toggListener to be an
		// instance of inner class ToggleListener, add
		// toggListener as an action listener to toggleButton
		toggListener = new ToggleListener();
		toggleButton.addActionListener(toggListener);

		// set the size for the instance of this GuiApplic2Frame
		setSize(250, 200);
		//pack();   // comment out setSize() call and uncomment this
					// to see how method pack() sizes the frame
					// based on the components within --- funky,
					// in this case, with FlowLayout...!
	} // end GuiApplic2Frame constructor

	// notice that these classes are WITHIN class GuiApplic2Frame; that's
	// what makes them *inner* classes
	//
	// notice that THESE have implements clause --- and actionPerformed();

	// this special listener handles counting button
	private class CountListener implements ActionListener
	{
		public void actionPerformed(ActionEvent event)
		{
			// because this is within class GuiApplic2,
			// it can "see" private data field count,
			// object countLabel
			count++;
			countLabel.setText("count clicks button pressed: " +
					   count + " times");
		}
	}

	// this special listener handles toggling the toggle
	// button's background color
	private class ToggleListener implements ActionListener
	{
		public void actionPerformed(ActionEvent event)
		{
			// and this can see object toggleButton,
			// data field nowGreen
			if (nowGreen)
			{
				toggleButton.setBackground(Color.orange);
				nowGreen = false;
			}
			else
			{
				toggleButton.setBackground(Color.green);
				nowGreen = true;
			}
		}
	}
}