Programming Drill #11

Due: Monday, June 9
Goal:  Practice with static/non-static variables

The Counter class below creates a JFrame window that implements a simple counter.   The window will look like this:

The program is a simple counter.  Clicking the "Plus 1" button adds one to the counter, which starts at 0. 

The program actually makes two instances of the Counter class, which creates two separate windows.  Initially the windows may be on top of one another, so you may have to move one to see the other.  Under some circumstances you might want the two classes to share the same counter.  For example, with one bank account there may be multiple ATM's where you go to access the shared bank account.  This is the purpose of the Refresh button - to re-display the counter in case it was changed by another window.  Or you might want the two classes to have separate counters.  The purpose of this drill is to have you explore how these two goals may be accomplished using a static instance variable.

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 (see code in the else-if below for how to do this)
        }
        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));
        }
    }
}
    

Fill in the "TO DO" parts of the code.  You don't have to worry about how the rest works.   First run the program using a static count variable and note the results.  Then run the program without a static count variable.  You should be able to explain to your instructor why you get different behavior.  If you prefer, you can download the code as a .java file: Counter.java