I am trying to make a calculator, but I can not seem to figure out how to get the = operation to work.
Everything else seems to be working fine, but I am just really struggling with the =.
Any guidance in the right direction would be super helpful!

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Calculator extends JFrame implements ActionListener
{
   public static final int WIDTH = 400;
   public static final int HEIGHT = 400;
   public static final int NUMBER_OF_DIGITS = 30;

   JTextField jb = new JTextField();
   private double result = 0.0;

   public static void main(String[] args)
   {
      Calculator aCalculator = new Calculator();
      aCalculator.setVisible(true);
   }

   public Calculator()
   {
      setTitle("Calculator");
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setSize(WIDTH, HEIGHT);

      JPanel textPanel = new JPanel();
      textPanel.setLayout(new GridLayout());
      jb = new JTextField ("", NUMBER_OF_DIGITS);
      jb.setBackground(Color.WHITE);

      // addButton
      JButton equalsButton = new JButton("=");
      equalsButton.addActionListener(this);
      // addButton
      JButton addButton = new JButton("+");
      addButton.addActionListener(this);
      // subtractButton
      JButton subtractButton = new JButton("-");
      subtractButton.addActionListener(this);
      // clearButton
      JButton clearButton = new JButton("C");
      clearButton.addActionListener(this);
      // multiplyButton
      JButton multiplyButton = new JButton("*");
      multiplyButton.addActionListener(this);
      // divideButton
      JButton divideButton = new JButton("/");
      divideButton.addActionListener(this);
      // button1
      JButton button1 = new JButton("1");
      button1.addActionListener(this);
      // button2
      JButton button2 = new JButton("2");
      button2.addActionListener(this);
      // button3
      JButton button3 = new JButton("3");
      button3.addActionListener(this);
      // button4
      JButton button4 = new JButton("4");
      button4.addActionListener(this);
      // button5
      JButton button5 = new JButton("5");
      button5.addActionListener(this);
      // button6
      JButton button6 = new JButton("6");
      button6.addActionListener(this);
      // button7
      JButton button7 = new JButton("7");
      button7.addActionListener(this);
      // button8
      JButton button8 = new JButton("8");
      button8.addActionListener(this);
      // button9
      JButton button9 = new JButton("9");
      button9.addActionListener(this);
      // button0
      JButton button0 = new JButton("0");
      button0.addActionListener(this);      
        setLayout(new GridLayout(5,1));
        JPanel r1 = new JPanel();
        r1.setBackground(Color.GRAY);
        JPanel r2 = new JPanel();
        r2.setBackground(Color.GRAY);
        JPanel r3 = new JPanel();
        r3.setBackground(Color.GRAY);
        JPanel r4 = new JPanel();
        r4.setBackground(Color.GRAY);
        JPanel r5 = new JPanel();
        r5.setBackground(Color.GRAY);
        add(r1);
        add(r2);
        add(r3);
        add(r4);
        add(r5);
        r1.setLayout(new GridLayout(1,1));
        r1.add(jb);
        r2.setLayout(new GridLayout(1,4));
        r2.add(button1);
        r2.add(button2);
        r2.add(button3);
        r2.add(addButton);
        r3.setLayout(new GridLayout(1,4));
        r3.add(button4);
        r3.add(button5);
        r3.add(button6);
        r3.add(subtractButton);
        r4.setLayout(new GridLayout(1,4));
        r4.add(button7);
        r4.add(button8);
        r4.add(button9);
        r4.add(divideButton);
        r5.setLayout(new GridLayout(1,4));
        r5.add(clearButton);
        r5.add(button0);
        r5.add(equalsButton);
        r5.add(multiplyButton);
   }

   public void actionPerformed(ActionEvent e)
   {
      try
      { 
         assumingCorrectNumberFormats(e);
      }
      catch (NumberFormatException e2)
      {
         jb.setText("Error: Reenter Number.");
      }
   }

   // Throws NumberFormatException
   public void assumingCorrectNumberFormats(ActionEvent e)
   {
      String actionCommand = e.getActionCommand();


      if(actionCommand.equals("1"))
      {
         String x = jb.getText();
         jb.setText(x+"1");
      }
      else if(actionCommand.equals("2"))
      {
         String x = jb.getText();
         jb.setText(x + "2");
      }
      else if(actionCommand.equals("3"))
      {
         String x = jb.getText();
         jb.setText(x + "3");
      }
      else if(actionCommand.equals("4"))
      {
         String x = jb.getText();
         jb.setText(x + "4");
      }
      else if(actionCommand.equals("5"))
      {
         String x = jb.getText();
         jb.setText(x + "5");
      }
      else if(actionCommand.equals("6"))
      {
         String x = jb.getText();
         jb.setText(x + "6");
      }
      else if(actionCommand.equals("7"))
      {
         String x = jb.getText();
         jb.setText(x + "7");
      }
      else if(actionCommand.equals("8"))
      {
         String x = jb.getText();
         jb.setText(x + "8");
      }
      else if(actionCommand.equals("9"))
      {
         String x = jb.getText();
         jb.setText(x + "9");
      }
      else if(actionCommand.equals("0"))
      {
         String x = jb.getText();
         jb.setText(x + "0");

      }
      else if(actionCommand.equals("+"))
      { 
         String x = jb.getText();
         jb.setText(x+"+");
      }
      else if(actionCommand.equals("-"))
      {
         String x = jb.getText();
         jb.setText(x + "-");
      }
      else if(actionCommand.equals("*"))
      { 
         String x = jb.getText();
         jb.setText(x + "*");
      }
      else if(actionCommand.equals("/"))
      { 
         String x = jb.getText();
         jb.setText(x + "/");
      }
      else if(actionCommand.equals("="))
      {
         String x = jb.getText();
         jb.setText(String.valueOf(x + "=" + result));
      }

      else if(actionCommand.equals("C"))
      {
         result = 0;
         jb.setText("");
      }
      else
         jb.setText("Unexpected error.");
   }

   // Throws NumberFormatException
   private static double stringToDouble(String stringObject)
   {
      return Double.parseDouble(stringObject.trim());
   }
}

Thenumbers are normally handled like you are, but when you get an operator you parse and store the previous number then make a note of the operator. Then when the next number is complete (signified by another operator or =) you can parse the seocnd number and do the arithmetic.
That's probably not clear (!)
What you need to do is turn off the computer and sit with some paper and a pen. Run through a number of test cases, writing down what you need to get the answer as you go. That way you will come to understand what needs to be strored and when to use it. Then you can go back to the program and complete it. This is ESSENTIAL. you MUST understand how the process works BEFORE you write the program.

Example:
Input is 12+10/2= (expected answer 11)
start with an empty string
1 append to the string
2 append to the string
+ parse the string, save the resulting number, clear the string, remember the +
1 append to the string
0 append to the string
/ parse and clear the string, recall the +, add the parsed value to the resulting number, forget the +, remember the /
2 append to the string
= parse and clear the string, recall the /, divide the result by the parsed value

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.