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 8809915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:59:39+00:00 2026-06-14T02:59:39+00:00

I am trying to wrap a Calculator GUI over a class that I built

  • 0

I am trying to wrap a Calculator GUI over a class that I built to calculate values in a different way (not the normal way, in reverse Polish notation).

I just wanted to know two things:

1) How do I write my actionPerformed method so that when you click on a button or operation, it shows up in the display

2) How do I go about using the GUI with the calculation method (like how to make it so that when you press all the numbers or operators on the GUI, it registers in my calculation class). Basically how to wrap the GUI over it?

**Edit, I edited my code, but when it compiles it gives me a null pointer exception, how come?

My GUI Class so far:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

public class GUI extends JFrame implements ActionListener
{
JPanel buttonPanel, topPanel, operationPanel;
JTextField display;

doMath math = new doMath();

JButton Num1;
JButton Num2;
JButton Num3;
JButton Num4;
JButton Num5;
JButton Num6;
JButton Num7;
JButton Num8;
JButton Num9;
JButton Num0;

JButton Add;
JButton Sub;
JButton Mult;
JButton Div;
JButton Eq;
JButton Clr;
public GUI()
{
    super("Calculator");
    setSize(400,400);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel mainPanel = new JPanel();
    mainPanel.setLayout(new GridLayout (2,1));

    buttonPanel = new JPanel();        
    buttonPanel.setLayout(new GridLayout(4, 4));
    buttonPanel.add(Num1 = new JButton("1"));
    buttonPanel.add(Num2 = new JButton("2"));
    buttonPanel.add(Num3 = new JButton("3"));
    buttonPanel.add(Num4 = new JButton("4"));
    buttonPanel.add(Num5 = new JButton("5"));
    buttonPanel.add(Num6 = new JButton("6"));
    buttonPanel.add(Num7 = new JButton("7"));
    buttonPanel.add(Num8 = new JButton("8"));
    buttonPanel.add(Num9 = new JButton("9"));
    buttonPanel.add(Num0 = new JButton("0"));        
    buttonPanel.add(Clr = new JButton("C"));
    buttonPanel.add(Eq = new JButton("="));
    buttonPanel.add(Add = new JButton("+"));
    buttonPanel.add(Sub = new JButton("-"));
    buttonPanel.add(Mult = new JButton("*"));
    buttonPanel.add(Div = new JButton("/"));


    Num1.addActionListener(this);
    Num2.addActionListener(this);
    Num3.addActionListener(this);
    Num4.addActionListener(this);
    Num5.addActionListener(this);
    Num6.addActionListener(this);
    Num7.addActionListener(this);
    Num8.addActionListener(this);
    Num9.addActionListener(this);
    Num0.addActionListener(this);
    Clr.addActionListener(this);
    Eq.addActionListener(this);
    Add.addActionListener(this);
    Sub.addActionListener(this);
    Mult.addActionListener(this);
    Div.addActionListener(this);

    topPanel = new JPanel();         
    topPanel.setLayout(new FlowLayout());
    topPanel.add(new JTextField(20));
   //jtfResult.setHorizontalAlignment(JTextField.RIGHT);
   // jtfResult.setEditable(false);  

    add(mainPanel);

    mainPanel.add(topPanel, BorderLayout.NORTH);
    mainPanel.add(buttonPanel, BorderLayout.SOUTH);

    setVisible(true);

}

public void actionPerformed(ActionEvent e) 
{
     if(e.getSource() == Num1)
    {
        s += "1";
        display.setText(s);
    }
    if(e.getSource() == Num2)
    {
        s += "2";
        display.setText(s);
    }
    if(e.getSource() == Num3)
    {
        s += "3";
        display.setText(s);
    }
    if(e.getSource() == Num4)
    {
        s += "4";
        display.setText(s);
    }
    if(e.getSource() == Num5)
    {
        s += "5";
        display.setText(s);
    }
    if(e.getSource() == Num6)
    {
        s += "6";
        display.setText(s);
    }
    if(e.getSource() == Num7)
    {
        s += "7";
        display.setText(s);
    }
    if(e.getSource() == Num8)
    {
        s += "8";
        display.setText(s);
    }
    if(e.getSource() == Num9)
    {
        s += "9";
        display.setText(s);
    }
    if(e.getSource() == Num0)
    {
        s += "0";
        display.setText(s);
    }
    if(e.getSource() == Add)
    {
        s += "+";
        display.setText(s);
    }
    if(e.getSource() == Sub)
    {
        s += "-";
        display.setText(s);
    }
    if(e.getSource() == Mult)
    {
        s += "*";
        display.setText(s);
    }
    if(e.getSource() == Div)
    {
        s += "/";
        display.setText(s);
    }
    if(e.getSource() == Eq)
    {
        String result = "" + math.doMath1(s);
        display.setText(result);
    }

}
}

My calculation class (works as intended to, just need to know how to wrap my GUI over it):

public class doMath
{
Stack stack = new Stack();

String next = "";

public doMath()
{

}

public int doMath1(String expr)
{
    while( expr.length()  >0) //for(int i = 0; i < expr.length(); i++)
{              
    if(expr.length() == 0)
        return 0;
    if(expr.indexOf(" ")>0)
    {
        next = expr.substring(0,expr.indexOf(" "));

        if(next.equals("+"))
            stack.push(stack.pop() + stack.pop());
        else if(next.equals("-"))
        {
            int x = stack.pop();
            stack.push(stack.pop()- x);
        }
        else if(next.equals("*"))
            stack.push(stack.pop() * stack.pop());
        else if(next.equals("/"))
        {
            int x = stack.pop();
            stack.push(stack.pop()/ stack.pop());
        }
        else 
        stack.push(new Integer(Integer.parseInt(next)));         
         expr = expr.substring(expr.indexOf(" ")+1);
    }        
}

    return stack.pop();
}    
}

Errors:

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at GUI.actionPerformed(GUI.java:126)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at     javax.swing.plaf.basic.BasicButtonListener.
    mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:5517)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3129)
at java.awt.Component.processEvent(Component.java:5282)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3984)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3819)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1791)
at java.awt.Component.dispatchEvent(Component.java:3819)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at         java.awt.EventDispatchThread.pumpOneEventForHierarchy
    (EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy
    (EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
  • 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-14T02:59:40+00:00Added an answer on June 14, 2026 at 2:59 am

    From your code, you seem to have a workable model.

    Basically you need to attach a ActionListener to each button. In your case, I’d use a single instance of the same listener…

    ActionListener calculatorActionHandler = new CalculatorActionHandler();
    Num1.addActionListener(calculatorActionHandler);
    

    In this handler I would take the text of the button and concatenate them together

    public class CalculatorActionHandler implements ActionListener {
        private StringBuilder expression = new StringBuilder(32);
        private Gui gui;
    
        public CalculatorActionHandler(Gui gui) {
            this.gui = gui;
        }
    
        public void actionPerformed(ActionEvent e) {
            JButton source = (JButton)e.getSource();
            String text = source.getText();
            if (text.equals("=")) {
                doMath math = new doMath();
                int result = math.doMath1(expression.toString());
                expession = new StringBuilder(32);
                // Update the UI
            } else {
                expession.append(text);
                // Update this value to the screen, maybe using a JLabel
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to build a calculator GUI using migLayout, but I'm not familiar with
I'm trying to wrap my head around the proper design to calculate an average
Trying to wrap my head around the jQuery .not() function, and running into a
I'm trying to wrap my head around Arquillian and am just not fundamentally understanding
I am trying to wrap a generated series of elements that are created on
Trying to wrap my head around django forms and the django way of doing
I'm trying to wrap a TextBlock's Text property (which is a string that is
I acknowledge that they can be useful, but I'm trying to wrap my head
I'm trying to calculate the width of an element so that when I use
I'm trying wrap my head around all of the different scripting technologies for Windows

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.