
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.FlowLayout;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 A button-click counter.
*/
public class Counter extends JFrame
                        implements ActionListener
{
    public static final int WIDTH = 200;
    public static final int HEIGHT = 100;

	// A label is able to display letters on the JFrame window
    private JLabel lblValue;

	// ****** TO DO: Create a private int variable here named count initialized to 0
	// ****** TO DO: First make this variable static and run the program.
	// ****** TO DO: Then remove the static and run the program.
	// ****** TO DO: Explain the different behavior.

    public static void main(String[] args)
    {
		// This creates two instances of Counter,
		// which creates two different windows.
        Counter counter1 = new Counter( );
        counter1.setVisible(true);

        Counter counter2 = new Counter( );
        counter2.setVisible(true);
    }

	// This is the constructor.  It is called automatically when
	// the object is created.
    public Counter( )
    {
        setTitle("Counter");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(WIDTH, HEIGHT);
        setLayout(new FlowLayout( ));

		// Create a label to display the counter
        lblValue = new JLabel("0");
        add(lblValue);
        // Create a button to add 1 to the counter
        JButton addButton = new JButton("Plus 1");
        addButton.addActionListener(this);
        add(addButton);
        // Create a button to refresh the counter
        JButton refreshButton = new JButton("Refresh");
        refreshButton.addActionListener(this);
        add(refreshButton);
    }

	// This method is called when you click on one of the
	// two buttons.  The if statement figures out which
	// button you clicked on.
    public void actionPerformed(ActionEvent e)
    {
		String actionCommand = e.getActionCommand( );

        if (actionCommand.equals("Plus 1"))
        {
			// ******** TO DO:  Write code here that increments count
			// ********         and then displays it in lblValue
		}
		else if (actionCommand.equals("Refresh"))
		{
			// When the refresh button is clicked
			// the line of code below converts the
			// "count" variable into a String and displays
			// it in the label on the window
        	lblValue.setText(Integer.toString(count));
		}
    }
}
