// 
// file: InterfaceExample.java  
// Implementing the ActionListener Interface
// 
// This example use displays a button and a label 
// on a JFrame. Every time the button is clicked 
// the label is updated
//
// This class implements the ActionListener interface
//

import javax.swing.*;		

// swing is the new GUI toolkit

import java.awt.*;			

// awt is the old gui toolkit

import java.awt.event.*;								

// The class itself implements ActionListener
// The SuperClass is the Swing frame - JFrame

public class InterFaceExample extends JFrame implements ActionListener {
	private static String labelPrefix = "number of button clicks: ";
	private int numClicks = 0;
	private JLabel label;
	
	//Constructor	

	public InterFaceExample() {

		// top level container is a JFrame
 
		super("InterFace Example");

		// create a Swing button and a Swing Label
		// for the button		

		JButton button = new JButton("Click Me");
		label = new JLabel(labelPrefix + "0   ");
 
		// register this class as an ActionListener

		button.addActionListener(this);

		/* add "space" between the top level container
 		 * and its contents using a JPanel with an
 		 * empty border - 30 pix per side in this example
		 */

 		JPanel pane = new JPanel();

		pane.setBorder(BorderFactory.createEmptyBorder(30,30,30,30));
		pane.add(button);		
		pane.add(label); 				

		/* add the JPanel to the JFrame		 
		 * Note that the Swing components have been added to
 		 * the JPanel component instead of the JFrame component
		 * where the JFrame contains the JPanel
		 */		

		this.getContentPane().add(pane,BorderLayout.CENTER);

		/*finish up the frame and show it
		 * watch out  ... this is tricky		 
		 * we actually define and write a little class
 		 * INSIDE the addWindowListener argument list
		 */		

		this.addWindowListener(new WindowAdapter(){
				public void windowClosing(WindowEvent e){
					System.exit(0);
				}
			}
		); 

		//closing parenthesis for addWindowListener

		this.pack();
		this.setVisible(true);
	} 

	//Constructor
	
	//implement the ActionListener interface
	// The Java runtime calls this method whenever the
 	// ActionEvent for our listener is generated

	public void actionPerformed(ActionEvent evt) {
		numClicks++;
		label.setText(labelPrefix + numClicks);
	}

	//main

	public static void main(String[] args) {
		new InterFaceExample();	
	}//main											

} //InterFaceExample
