I'm trying to make a calculator.
I know how to handle it if the user hits 1 and + and 1 and then equals, but how do I get it to keep storing the values if the user enters 1+1 * 3 / 2 = (read (1+1) * 3)/2)?
package calculator; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; public class MyCalculator extends JFrame { private JButton zero, one, two, three, four, five, six, seven, eight, nine, times, plus, minus, divide, clear, equals, decimal, percent; private JTextField numbers; private JPanel panel; public MyCalculator() { super("Basic Calculator"); setVisible(true); panel = new JPanel(); panel.setVisible(true); zero = new JButton("0"); panel.add(zero); zero.setLocation(160, 160); zero.setVisible(true); one = new JButton("1"); panel.add(one); one.setLocation(190, 160); one.setVisible(true); two = new JButton("2"); panel.add(two); two.setLocation(220, 160); two.setVisible(true); numbers = new JTextField(12); panel.add(numbers); numbers.setLocation(60, 60); numbers.setVisible(true); numbers.setText("0"); zero.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub String values = numbers.getText(); values = values + "0"; numbers.setText(values); } }); one.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub String values = numbers.getText(); values = values + "1"; numbers.setText(values); } }); two.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub String values = numbers.getText(); values = values + "2"; numbers.setText(values); } }); } }