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 2015-09-16 */ public class CountClicksTest { /** * creates a CountClicks frame * * @param args not used here */ public static void main(String args[]) { EventQueue.invokeLater( new Runnable() { public void run() { 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 = 150; private final static int DEFAULT_HEIGHT = 100; /** * constructs a count-clicks frame instance */ public CountClicksFrame() { this.setTitle("Button-click Counter"); this.setSize(this.DEFAULT_WIDTH, this.DEFAULT_HEIGHT); // add count-clicks panel to frame CountClicksPanel panel = new CountClicksPanel(); this.add(panel); } } /** * 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 /** * 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); JButton clickMe = new JButton("Click Me"); clickMe.setForeground(Color.BLUE); this.showNumClicks = new JLabel("# of clicks: " + this.numClicks); // 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) { CountClicksPanel.this.numClicks++; CountClicksPanel.this.showNumClicks.setText( "# of clicks: " + CountClicksPanel.this.numClicks); } } } // end of class CountClicksPanel