Computer Science 523
Advanced Programming

Summer 2014, The College of Saint Rose

ButtonClickCounterPanels Demo

A working demo of ButtonClickCounterPanels will appear below. Click inside the applet to interact with it.



ButtonClickCounterPanels BlueJ Project

Click here to download a BlueJ project for ButtonClickCounterPanels.


ButtonClickCounterPanels Source Code

The Java source code for ButtonClickCounterPanels is below. Click on a file name to download it.


ButtonClickCounterPanels.java

/*
 * Example ButtonClickCounterPanels: adding some JPanels to make it
 * look a bit nicer.
 *
 * Jim Teresco, The College of Saint Rose, CSC 523, Summer 2014
 *
 * $Id: ButtonClickCounterPanels.java 2379 2014-06-17 03:52:55Z terescoj $
 */


import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class ButtonClickCounterPanels extends JApplet implements ActionListener {
    
    // Just as with other custom classes, we can introduce instance 
    // variables that have a class scope and which will retain their
    // values throughout the life of this class
    private int clickCount;
    private JLabel clickCountLabel;
    
    public void init() {
        
        // we can set the startup size of our window
        setSize(300, 150);
        
        Container contentPane = getContentPane();
        
        // In this init method, we'll add a JLabel and a JButton to our pane.
        // Note that we only need the JButton reference within the init method,
        // so it is a local variable, but we'll need the JLabel later, so
        // it is an instance varable.
        JButton button = new JButton("Press Here!");
        
        // we'll put the button into a JPanel, which will allow
        // it to be a more reasonable size, and then the JPanel
        // into the content pane
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(button);
        contentPane.add(buttonPanel, BorderLayout.SOUTH);
        
        // The key to event-driven programming: we tell the button who
        // to tell when someone presses it.  We specify an instance of
        // a class that implements ActionListener, which means that the
        // class is guaranteed to have an actionPerformed method.  Here,
        // we specify "this", as the current class implements ActionListener
        // so we want the button to call the actionPerformed method of
        // this class when it gets pressed.
        button.addActionListener(this);
        
        clickCount = 0;
        clickCountLabel = new JLabel("0 clicks");

        // Changing the font of a JLabel:
        clickCountLabel.setFont(new Font("Serif", Font.BOLD, 36));
        JPanel labelPanel = new JPanel();
        labelPanel.add(clickCountLabel);
        contentPane.add(labelPanel, BorderLayout.CENTER);
        
    }
    
    // This method is the one that will be called when the button is pressed.
    // By stating "implements ActionPerformed" in the class header, we are
    // promising Java that we'll be providing this exact method.
    public void actionPerformed(ActionEvent e) {
        
        // if someone presses the button, we'll increment our click counter
        // and update the label to show the new count
        clickCount++;
        if (clickCount == 1) {
            clickCountLabel.setText("1 click");
        }
        else {
            clickCountLabel.setText(clickCount + " clicks");
        }
        
    }
}