I am pretty fresh in the java environment but i am working hard to learn. I wrote the code for a calculator, however I am having problems in making the memory buttons function properly. I have to create 4 memory buttons that function in the following way: When the memory button is pushed once, whatever value is in the textfield has to be remembered. When the same memory button is pushed again, the remembered value has to be placed into the text field, and so on. The major conflict is that all four buttons must store and draw their values from the same array. If you can please help me with this, give any suggestions !! Your help is very much appreciated.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
//**************************************************************************************************
public class Calculator extends JFrame implements ActionListener
{
public static final int WIDTH = 225;
public static final int HEIGHT = 225;
public static final int ROWS = 5;
public static final int COLUMNS = 4;
private JTextField display;
private boolean transform = false;
private String equal = "=";
private double argum = 0;
//**************************************************************************************************
// This constructor creates the GUI.
public Calculator()
{
setTitle("Calculator");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(WIDTH,HEIGHT);
Container cp = getContentPane();
cp.setLayout(new BorderLayout());
display = new JTextField("0");
display.setEditable(false);
cp.add(display, "North");
JPanel bp = new JPanel();
bp.setLayout(new GridLayout (ROWS,COLUMNS));
String buttons = "789/456*123-0.=+";
for (int n = 0; n < buttons.length(); n++)
addButtons(bp, buttons.substring(n, n + 1));
JButton memo1 = new JButton("M1");
bp.add(memo1);
memo1.addActionListener(this);
JButton memo2 = new JButton("M2");
bp.add(memo2);
memo2.addActionListener(this);
JButton memo3 = new JButton("M3");
bp.add(memo3);
memo3.addActionListener(this);
JButton memo4 = new JButton("M4");
bp.add(memo4);
memo4.addActionListener(this);
cp.add(bp, "Center");
}
private void addButtons(Container con, String str)
{
JButton but = new JButton(str);
con.add(but);
but.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
String str = e.getActionCommand();
double[] memo = new double[4];
if ('0' <= str.charAt(0) && str.charAt(0) <= '9'
|| str.equals("."))
{
if (transform) display.setText(str);
else display.setText(display.getText() + str);
transform = false;
}
else
{
double val = (Double.valueOf(display.getText())).doubleValue();
calculate(val);
equal = str;
//These memory buttons functions are not working !!
if (str.equals("M1")) memo[0] = (Double.valueOf(display.getText())).doubleValue();
{if (equal.equals("M1")) display.setText(""+memo[0]);}
transform = true;
}
}
public void calculate(double num)
{
if (equal.equals("+")) argum += num;
else if (equal.equals("-")) argum -= num;
else if (equal.equals("*")) argum *= num;
else if (equal.equals("/")) argum /= num;
else if (equal.equals("=")) argum = num;
display.setText("" + argum);
}
//**************************************************************************************************
// This is the main of the class Calculator.
public static void main(String[] args)
{
JFrame f = new Calculator();
f.setVisible(true);
} // end of main.
} // end of class Calculator.