//******************************************************************** // PushCounterPanel.java Authors: Lewis/Loftus // // Demonstrates a graphical user interface and an event listener. //******************************************************************** import java.awt.*; import java.awt.event.*; import javax.swing.*; public class PushCounterPanel extends JPanel { private int count; private JButton push; private JLabel label; //----------------------------------------------------------------- // Constructor: Sets up the GUI. //----------------------------------------------------------------- public PushCounterPanel () { count = 0; push = new JButton ("Push Me!"); push.addActionListener (new ButtonListener()); label = new JLabel ("Pushes: " + count); add (push); add (label); setPreferredSize (new Dimension(300, 40)); setBackground (Color.cyan); } //***************************************************************** // Represents a listener for button push (action) events. //***************************************************************** private class ButtonListener implements ActionListener { //-------------------------------------------------------------- // Updates the counter and label when the button is pushed. //-------------------------------------------------------------- public void actionPerformed (ActionEvent event) { count++; label.setText("Pushes: " + count); } } }