943,544 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 4014
  • Java RSS
Apr 29th, 2004
0

Memory buttons problem

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
redelao is offline Offline
1 posts
since Apr 2004
May 18th, 2004
0

Re: Memory buttons problem

One reason why your memory buttons don't work: The array you're using to store the values is declared in the actionPerformed method. This means that as soon as the method returns, the array goes away forever. If you move the declaration somewhere with a larger scope, e.g. make it a member of your Calculator class, the stored values will stick around after the action processing has finished.
Reputation Points: 182
Solved Threads: 71
Posting Pro in Training
gusano79 is offline Offline
475 posts
since May 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Java Applet viewer blinks everytime the init method is used.
Next Thread in Java Forum Timeline: Creating Java interface to my c++ program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC