Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8550011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:51:13+00:00 2026-06-11T13:51:13+00:00

/* I am trying to make a basic calculator in JAVA using swings. However,

  • 0
/* 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();
        }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T13:51:14+00:00Added an answer on June 11, 2026 at 1:51 pm

    You first declare the buttons as member variables and then you use (declare again and initialize) the same variable names inside the constructor. This means that the member variables remain all equal to null. To fix this, just drop the leading JButton from the buttons initialization in the constructor.

    JButton mul = new JButton("Multiply");
    

    becomes

    mul = new JButton("Multiply");
    

    The issue related to variable names that you had here is known as shadowing.

    And the full solution. Please note the changes to the class name, the variable declaration, formatting,….

    import java.awt.Container;
    import java.awt.GridLayout;
    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.JTextField;
    
    public class Calculator extends JApplet implements ActionListener {
        private JLabel num1_label;
        private JLabel num2_label;
        private JLabel result_label;
        private JTextField t1;
        private JTextField t2;
        private JTextField t3;
        private JButton sum;
        private JButton sub;
        private JButton mul;
        private JButton div;
        private float num1;
        private float num2;
        private float res;
    
        public Calculator() {
            Container contentPanel = getContentPane();
            contentPanel.setLayout(new GridLayout(6, 2));
    
            num1_label = new JLabel("Number 1");
            contentPanel.add(num1_label);
    
            t1 = new JTextField(10);
            contentPanel.add(t1);
    
            num2_label = new JLabel("Number 2");
            contentPanel.add(num2_label);
    
            t2 = new JTextField(10);
            contentPanel.add(t2);
    
            result_label = new JLabel("Result");
            contentPanel.add(result_label);
    
            t3 = new JTextField(10);
            contentPanel.add(t3);
    
            sum = new JButton("Add");
            sum.addActionListener(this);
            contentPanel.add(sum);
    
            sub = new JButton("Subtract");
            sub.addActionListener(this);
            contentPanel.add(sub);
    
            mul = new JButton("Multiply");
            mul.addActionListener(this);
            contentPanel.add(mul);
    
            div = new JButton("Divide");
            div.addActionListener(this);
            contentPanel.add(div);
        }
    
        public void actionPerformed(ActionEvent ae) {
            System.out.println("hhhhh");
            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();
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make a very basic game with Java and I am
I've been trying to make my own basic .obj (Wavefront) renderer using the OpenGL
Hey guys I'm trying to make a basic calculator (polish style) but can't figure
I'm trying to make a basic client-server application for windows phone 7 (using Mango
I'm trying to make a basic datagram client/server program with Java. I've made the
I am trying to make a Galaga type game in android using the basic
I'm trying to make a basic inventory system in java . The items that
I'm just trying to make the basic Selenium2Example work with Firefox 4 using Maven
I am trying to make a basic java console based menu system. I have
I am trying to make a basic quotation-sharing app using Webapp. Obviously it is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.