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

/**
 * A GUI application with a button for which the number
 * of clicks is kept track of and displayed
 * 
 * @author Sharon Tuttle
 * @version 2021-09-26
 */

public class CountClicksTest 
{
    /**
     * creates a CountClicks frame
     *
     * @param args not used here
     */
    
    public static void main(String args[])
    { 
        EventQueue.invokeLater(
            () ->
                {
                    CountClicksFrame mainFrame = 
                        new CountClicksFrame();
                    mainFrame.setDefaultCloseOperation( 
                        JFrame.EXIT_ON_CLOSE );    
                    mainFrame.setVisible(true);    
                } );
    }
}

/** 
 * A frame with a panel containing two labels and a button
 */

class CountClicksFrame extends JFrame
{
    // data fields
    
    private final static int DEFAULT_WIDTH = 250;
    private final static int DEFAULT_HEIGHT = 125;
    
    /**
     * constructs a count-clicks frame instance
     */
    
    public CountClicksFrame()
    {    
        setTitle("Button-click Counter");
        setSize(this.DEFAULT_WIDTH, this.DEFAULT_HEIGHT);

        // add count-clicks panel to frame
        
        CountClicksPanel ccPanel = new CountClicksPanel();
        this.add(ccPanel);
    }
}

/**
 * A panel with two labels and a button, that displays how many times
 * the button has been clicked
 */

class CountClicksPanel extends JPanel
{
    // data fields for CountClicksPanel
    
    private int numClicks;         // keeps track of the number of button clicks
    private JLabel showNumClicks;  // displays the number of button clicks

    // font to be used throughout this panel

    private static final Font CC_FONT = new Font("SanSerif", Font.PLAIN, 20);
    
    /**
     * constructs a count-clicks panel instance
     */
    
    public CountClicksPanel()
    {
        // button has not been clicked yet
        
        this.numClicks = 0;
        
        // create the labels and button for this panel
        
        JLabel welcome = new JLabel("Button-Click Counter");
        welcome.setForeground(Color.BLUE);
        welcome.setFont(CC_FONT);

        JButton clickMe = new JButton("Click Me");
        clickMe.setForeground(Color.BLUE);
        clickMe.setFont(CC_FONT);
        
        this.showNumClicks = new JLabel("# of clicks: " + 
                                   this.numClicks);
        this.showNumClicks.setFont(CC_FONT);
        
        // add labels and button to this panel
        
        this.add(welcome);
        this.add(clickMe);
        this.add(showNumClicks);
        
        // create button action (so clicks will be counted),
        //    and associate that action with clickMe button
        
        CountClicksAction countAction = new CountClicksAction();
        clickMe.addActionListener(countAction);

    } // end CountClicksPanel constructor  
    
    /**
     * An action listener that counts the number of times the
     * clickMe button has been clicked
     */
    
    private class CountClicksAction implements ActionListener
    {   
        // default constructor will suffice, in this case
        
        /**
         * increases and displays the number of clicks of this
         * button
         * 
         * @param event a click of the clickMe button
         */
        
        public void actionPerformed(ActionEvent event)
        {
            numClicks++;
            showNumClicks.setText(
                "# of clicks: " + numClicks);
        }
    }
} // end of class CountClicksPanel