title: Java: JButtons not working with action Listener
body: <pre><code>import java.awt.Graphics;
  import javax.swing.JApplet;
  import javax.swing.JOptionPane;
  import java.awt.*;
  import javax.swing.*;
  import java.applet.*;
  import java.awt.event.*;
  
  public class JustinSodaMachine extends JApplet
  {
     private JButton coke;
     private JButton dietCoke;
     private JButton mellowYellow;
     private JButton water;
     private JButton cherryCoke;
     private Panel aPanel;
     private Panel bPanel;
     private Panel cPanel;
     private int cokeNum = 0;
     private int dietCokeNum = 0;
     private int mellowYellowNum = 0;
     private int waterNum = 0;
     private int cherryCokeNum = 0;
     private double amount;
     private double change;
  
  
     private JTextField amountIn;
  
     public void init()
     {
        setLayout(new BorderLayout());
        buildAPanel();
        buildBPanel();
        buildCPanel();
        add(aPanel,BorderLayout.CENTER);
        add(bPanel,BorderLayout.NORTH);
        add(cPanel,BorderLayout.SOUTH);
  
  
           }
  
     private void buildAPanel()
     {
  
        aPanel = new Panel();
        Button coke = new Button("Coke");
        Button dietCoke = new Button("Diet Coke");
        Button mellowYellow = new Button("Mellow Yellow");
        Button water = new Button("Water");
        Button cherryCoke = new Button("Cherry Coke");
        aPanel.setLayout(new GridLayout(5, 1, 10, 40));
  
        coke.addActionListener(new ButtonHandler());
        dietCoke.addActionListener(new ButtonHandler());
        mellowYellow.addActionListener(new ButtonHandler());
        water.addActionListener(new ButtonHandler());
        cherryCoke.addActionListener(new ButtonHandler());
  
        aPanel.add(coke);
        aPanel.add(dietCoke);
        aPanel.add(mellowYellow);
        aPanel.add(water);
        aPanel.add(cherryCoke);
        setVisible(true);
  
     }
  
  
     private void buildBPanel()
     {
        bPanel = new Panel();
        JLabel title = new JLabel("Justin's Soda Machine Drinks 0.75 Cent");
        bPanel.add(title);
        setVisible(true);
     }
  
     private void buildCPanel()
     {
        cPanel = new Panel();
        JLabel amountText = new JLabel("Amount deposited: ");
        amountIn = new JTextField(10);
        amountIn.setText("0");
        amountIn.setEditable(true);
  
  
        cPanel.add(amountText);
        cPanel.add(amountIn);
        setVisible(true);
      }
  
  
  
  private class ButtonHandler implements ActionListener
  {
        public void actionPerformed(ActionEvent e)
        {
  
  
           double balance = Double.parseDouble(amountIn.getText());
  
           if(balance &lt; 0.75)
           JOptionPane.showMessageDialog(null, "Inadequate amount of money ");
  
           else
           {
  
              if(e.getSource() == coke )
              {
                 cokeNum++;
                 if(cokeNum &lt;= 20)
                 {
                    balance = (amount - 0.75);
                    JOptionPane.showMessageDialog(null, "The balance is : " + balance + " You selected Cola");
                 }
                 else
                    JOptionPane.showMessageDialog(null, "Out of range ");
              }
  
              if(e.getSource() == dietCoke)
              {
                 dietCokeNum++;
                 if(dietCokeNum &lt;= 20)
                 {
                    balance = (amount - 0.75);
                    JOptionPane.showMessageDialog(null, "The balance is : " + balance + " You selected Lemon-line soda" );
                 }
                 else JOptionPane.showMessageDialog(null, "Out of range ");
              }
  
              if(e.getSource() == mellowYellow )
              {
                 mellowYellowNum++;
                 if(mellowYellowNum &lt;= 20)
                 {
                    balance = (amount - 0.75);
                    JOptionPane.showMessageDialog(null, "The balance is : " + balance + " You selected Grape soda" );
                 }
                 else JOptionPane.showMessageDialog(null, "Out of range ");
              }
  
              if(e.getSource() == water )
              {
                 waterNum++;
                 if(waterNum &lt;= 20)
                 {
                    balance = (amount - 0.75);
                    JOptionPane.showMessageDialog(null, "The balance is : " + balance + " You selected Dirnk Root beer");
                 }
                 else JOptionPane.showMessageDialog(null, "Out of range ");
              }
  
  
              if(e.getSource() == cherryCoke )
              {
                 cherryCokeNum++;
                 if(cherryCokeNum &lt;= 20)
                 {
                    balance = (amount - 0.75);
                    JOptionPane.showMessageDialog(null, "The balance is : " + balance + " You selected bottle of water");
                 }
                 else JOptionPane.showMessageDialog(null, "Out of range ");
              }
              amountIn.setText(""+balance);
           }
  
        }
     }
  }
  </code></pre>
  
  <p>I have tried everything I can think of to make the buttons work for this vending machine to recognize the drink selection and subtract 0.75 cents from the total deposited. I have no clue why this is not working and any help is appreciated. It complies fine in jgrasp with no errors or exceptions.</p>