title: How to increase value by 1 each time a JButton has been clicked?
body: <p>I've just learnt about <code>ActionListener</code> and my task is to create a simple application using a <code>JButton</code> associated with the <code>JLabel</code> which displays an <code>int</code> to be increased by the value of 1 each time it has been clicked. Here's my coding:</p>
  
  <pre><code>import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;
  
  class ButtonPanel2 extends JPanel implements ActionListener {
  
  JButton button2;
  JLabel secondLabel;
  
  int i = 1;
  int total = 0;
  
  ButtonPanel2()
  {
      button2 = new JButton("Push");
      add(button2);
  
      button2.addActionListener(this);
  
      secondLabel = new JLabel("Button pushes: " + total);
      add(secondLabel);
  }
  
  public void actionPerformed(ActionEvent e)
  {
      total += i;
  }
  }
  </code></pre>
  
  <p>And the driver class:</p>
  
  <pre><code>import java.awt.*;
  import javax.swing.*;
  
  class TestButtonPanel2 {
  
  public static void main(String [] args)
  {
      JFrame buttonFrame2 = new JFrame("Button Panel 2");
  
      ButtonPanel2 panel2 = new ButtonPanel2();
  
      buttonFrame2.add(panel2);
      buttonFrame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      buttonFrame2.setSize(400 , 400);
      buttonFrame2.setVisible(true);
  }
  }
  </code></pre>
  
  <p>Thanks in advance! :)</p>