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

/**
 *  Creates and displays a simple GUI using Java Swing
 *
 *  @author  Nick Howe
 *  @version 17 September 2008
 */
public class GUIdemo {
    /** Label is part of GUI state because we will change its text */
    JLabel label;

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    public void createAndShowGUI() {
        // Make sure we have nice window decorations.
        JFrame.setDefaultLookAndFeelDecorated(true);

        // Create and set up the window.
        JFrame frame = new JFrame("JCircleGUIdemo Application");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.getContentPane().setLayout(new GridLayout(2,2));

	// Add components
	createComponents(frame.getContentPane());

        // Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    /**
     *  Creates and lays out all the components for the GUI
     *
     *  @param pane  The container where everything is to be created
     */
    public void createComponents(Container pane) {
	// create and lay out the components
	pane.setLayout(new FlowLayout());
	JButton button = new JButton("Test");
	ToggleListener tlistener = new ToggleListener();
	button.addActionListener(tlistener);
	pane.add(button);
	label = new JLabel("Off");
	pane.add(label);

	// add the listener for the button
    }

    /** This is the nested listener class for the toggle button */
    private class ToggleListener implements ActionListener {
	/** 
	 *  When the button is clicked, toggle the label state
	 *
	 *  @param e  Describes the event
	 */
	public void actionPerformed(ActionEvent e) {
	    if (label.getText().equals("On")) {
		label.setText("Off");
	    } else {
		label.setText("On");
	    }
	}
    }
}
