import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ButtonOnAFrame extends JFrame implements ActionListener {

	JButton button, exitButton;

	/**
	 * @param args
	 */
	public ButtonOnAFrame() {
		setTitle("Button on a Frame");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JPanel panel = new JPanel();
		panel.setPreferredSize(new Dimension(200, 200));
		panel.setBackground(Color.RED);

		button = new JButton("000");
		exitButton = new JButton("Exit");
		//Which object handles action events on this button?
		button.addActionListener(this);
		exitButton.addActionListener(this);

		panel.add(button);
		panel.add(exitButton);

		getContentPane().add(panel);
		pack();
		setVisible(true);

	}

	public static void main(String[] args) {
		ButtonOnAFrame bof = new ButtonOnAFrame();

	}

	// This is the event handler for the button
	public void actionPerformed(ActionEvent eventObject) {
		Object o = eventObject.getSource();

		if (o instanceof JButton) //a button press on some JButton triggered the event
		{
			if (o == button) {
				String s = eventObject.getActionCommand();
				int currentValue = Integer.parseInt(s);
				button.setText(new Integer(currentValue + 1).toString());
			} else if (o == exitButton) {
				System.exit(0);
			}
		}
	}

}
