title: Java Scientific calculator GUI
body: <p>I am working on a calculator with a GUI. I'm not sure how I should gather all the numbers from the user and do the math for them. I tried storing them in variables, but that doesn't really work since I need to be able to have multiple numbers and operators at one time. Any input would be greatly appreciated. Thanks!</p>
  <pre><code>import java.util.*;
  import javax.swing.*;
  import java.awt.*;
  import java.awt.event.*;
  
  public class GUI extends JFrame implements ActionListener {
      private JFrame frame;
      private JPanel buttons;
  
      //
      private JPanel text;
      private JTextField field;
  
      //
      private ImageIcon WCU;
      private JLabel labelWCU;
  
      ///
      JButton[] nums = new JButton[10];
      JButton[] functions = new JButton[15];
  
      ArrayList&lt;Double&gt; Addition = new ArrayList&lt;&gt;();
  
      double A;
      double S;
      double M;
      double D;
      double Answer;
  
      private JButton num1, num2, num3, num4, num5, num6, num7, num8, num9, num0;
      private JButton decimal, add, subtract, multiply, divide, equals, squareRoot;
      private JButton sin, cos, tan, exclamation, oneOverX, xSquared, log;
      private JButton clear;
  
      double input1 = 0, input2 = 0, answer = 0;
      char op;
  
      public GUI() {
          frame = new JFrame();
          buttons = new JPanel();
  
          //
          text = new JPanel();
          field = new JTextField(30);
  
          //
          WCU = new ImageIcon(&quot;WCU.png&quot;);
          labelWCU = new JLabel(WCU);
          
  
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setTitle(&quot;Calculator&quot;); // make sure to center
          frame.setSize(525,750);
          frame.setLocationRelativeTo(null);
          frame.setResizable(false);
          frame.setLayout(new BorderLayout());
          frame.add(text, BorderLayout.NORTH);
          frame.add(buttons, BorderLayout.CENTER);
  
          text.add(field);
          field.setEditable(false);
          text.setBackground(Color.RED);
          
  
          buttons.setBackground(Color.red);
          buttons.setBorder(BorderFactory.createEmptyBorder(20,50,300,50)); 
          buttons.setLayout(new GridLayout(6,4,20,20));
  
          //  
          buttons.add(decimal = new JButton (&quot;.&quot;));
          functions[0] = decimal;
  
          buttons.add(add = new JButton (&quot;+&quot;));
          functions[1] = add;
  
          buttons.add(subtract = new JButton (&quot;-&quot;));
          functions[2] = subtract;
  
          buttons.add(multiply = new JButton (&quot;*&quot;));
          functions[3] = multiply;
  
          buttons.add(divide = new JButton (&quot;/&quot;));
          functions[4] = divide;
  
          buttons.add(equals = new JButton (&quot;=&quot;));
          functions[5] = equals;
  
          buttons.add(squareRoot = new JButton (&quot;√&quot;));
          functions[6] = squareRoot;
  
          buttons.add(num1 = new JButton (&quot;1&quot;));
          nums[1] = num1;
  
          buttons.add(num2 = new JButton (&quot;2&quot;));
          nums[2] = num2;
          
          buttons.add(num3 = new JButton (&quot;3&quot;));
          nums[3] = num3;
  
          buttons.add(num4 = new JButton (&quot;4&quot;));
          nums[4] = num4;
  
          buttons.add(num5 = new JButton (&quot;5&quot;));
          nums[5] = num5;
  
          buttons.add(num6 = new JButton (&quot;6&quot;));
          nums[6] = num6;
  
          buttons.add(num7 = new JButton (&quot;7&quot;));
          nums[7] = num7;
  
          buttons.add(num8 = new JButton (&quot;8&quot;));
          nums[8] = num8;
  
          buttons.add(num9 = new JButton (&quot;9&quot;));
          nums[9] = num9;
  
          buttons.add(num0 = new JButton (&quot;0&quot;));
          nums[0] = num0;
  
          buttons.add(sin = new JButton (&quot;sin&quot;));
          functions[7] = sin;
  
          buttons.add(cos = new JButton (&quot;cos&quot;));
          functions[8] = cos;
  
          buttons.add(tan = new JButton (&quot;tan&quot;));
          functions[9] = tan;
  
          buttons.add(exclamation = new JButton (&quot;!&quot;));
          functions[10] = exclamation;
  
          buttons.add(oneOverX = new JButton (&quot;1/x&quot;));
          functions[11] = oneOverX;
  
          buttons.add(xSquared = new JButton (&quot;x^2&quot;));
          functions[12] = xSquared;
  
          buttons.add(log = new JButton (&quot;log&quot;));
          functions[13] = log;
  
          text.add(clear = new JButton(&quot;Clear&quot;));
          functions[14] = clear;
  
          labelWCU.setIcon(WCU);
  
          frame.setVisible(true);
  
          //
          for (int i = 0; i &lt; 15; i++){
              functions[i].addActionListener(this);
              functions[i].setFocusable(false);
          }
  
          for (int i = 0; i &lt; 10; i++){
              nums[i].addActionListener(this);
              nums[i].setFocusable(false);
          }
  
      }
      
      public void actionPerformed(ActionEvent e){
  
          for (int i = 0; i &lt; 10; i++){
              if(e.getSource() == nums[i]){
                  field.setText(field.getText().concat(String.valueOf(i)));
              }
          }
  
          if(e.getSource() == decimal){
              field.setText(field.getText().concat(&quot;.&quot;));
          }
  
          if(e.getSource() == add){
              A = Double.parseDouble(field.getText());
              op = '+';
              field.setText(&quot;&quot;);
          }
  
          if(e.getSource() == subtract){
              input1 = Double.parseDouble(field.getText());
              op = '-';
              field.setText(&quot;&quot;);
          }
  
          if(e.getSource() == multiply){
              input1 = Double.parseDouble(field.getText());
              op = '*';
              field.setText(&quot;&quot;);
          }
  
          if(e.getSource() == divide){
              input1 = Double.parseDouble(field.getText());
              op = '/';
              field.setText(&quot;&quot;);
          }
  
          if(e.getSource() == equals){
              
              field.setText(String.valueOf(Addition));
          }
  
          if(e.getSource() == clear){
              field.setText(&quot;&quot;);
              A = 0;
          }
  
      }
  }
  </code></pre>