title: ActionListener in Java
body: <pre><code>/* I am trying to make a basic calculator in JAVA using swings. However, after adding
   * actionlisteners to each of my buttons, the actionPerformed method is never called. I can't
   *seem to figure out why .
   */
  
  import javax.swing.*;
  import java.awt.*;
  import java.awt.event.*;
  
  public class calculator extends JApplet implements ActionListener
  {
           JLabel num1_label,num2_label,result_label;
           JTextField t1,t2,t3;
           JButton sum,sub,mul,div;     
           float num1,num2,res;
  
      public calculator ()
      {
          Container contentPanel = getContentPane();
          contentPanel.setLayout(new GridLayout(6,2));
  
          JLabel num1_label = new JLabel("Number 1");
          contentPanel.add(num1_label);
  
          JTextField t1 = new JTextField(10);
          contentPanel.add(t1);
  
          JLabel num2_label = new JLabel("Number 2");
          contentPanel.add(num2_label);
  
          JTextField t2 = new JTextField(10);
          contentPanel.add(t2);
  
          JLabel result_label = new JLabel("Result");
          contentPanel.add(result_label);
  
          JTextField t3 = new JTextField(10);
          contentPanel.add(t3);
  
          JButton sum = new JButton("Add");      
          sum.addActionListener(this);
          contentPanel.add(sum);
  
          JButton sub = new JButton("Subtract");
          sub.addActionListener(this);
          contentPanel.add(sub);
  
          JButton mul = new JButton("Multiply");
          mul.addActionListener(this);
          contentPanel.add(mul);
  
          JButton div = new JButton("Divide");
          div.addActionListener(this);
          contentPanel.add(div);
  
          }
  
          public void actionPerformed(ActionEvent ae)
          {
              if(ae.getSource() == sum)
                {
                    addition();
                }
  
              if(ae.getSource() == sub)
              {
                  subtraction();
              }
  
              if(ae.getSource() == mul)
              {
                  multiplication();
              }
  
              if(ae.getSource() == div)
              {
                  division();
              }
  
          }
  
          public void addition()
          {
              try {
                    num1 = Float.parseFloat(t1.getText());
                    num2 = Float.parseFloat(t2.getText());
                    res  = num1 + num2;                
                    t3.setText(" " + res);
                  }
                  catch (NumberFormatException nfe) 
                  {
                      t1.setText("0");
                      t2.setText("0");
                      t3.setText("0");
                  }
  
          }
  
           public void subtraction()
          {
              try {
                    num1 = Float.parseFloat(t1.getText());
                    num2 = Float.parseFloat(t2.getText());
                    res  = num1 - num2;                
                    t3.setText(" " + res);
                  }
                  catch (NumberFormatException nfe) 
                  {
                      t1.setText("0");
                      t2.setText("0");
                      t3.setText("0");
                  }
  
          } 
  
          public void multiplication()
          {
              try {
                    num1 = Float.parseFloat(t1.getText());
                    num2 = Float.parseFloat(t2.getText());
                    res  = num1 * num2;                
                    t3.setText(" " + res);
                  }
                  catch (NumberFormatException nfe) 
                  {
                      t1.setText("0");
                      t2.setText("0");
                      t3.setText("0");
                  }
  
          }
  
          public void division()
          {
              try {
                    num1 = Float.parseFloat(t1.getText());
                    num2 = Float.parseFloat(t2.getText());
                    res  = num1 / num2;                
                    t3.setText(" " + res);
                  }
                  catch (NumberFormatException nfe) 
                  {
                      t1.setText("0");
                      t2.setText("0");
                      t3.setText("0");
                  }
  
          }
  
          public static void main(String argsp[])
          {
              calculator c = new calculator();
          }
  
  }
  </code></pre>