| | |
Memory buttons problem
![]() |
•
•
Join Date: Apr 2004
Posts: 1
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: May 2004
Posts: 71
Reputation:
Solved Threads: 4
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.
![]() |
Similar Threads
- C++ difference between memory management in Debug vs. Release: data loss problem (C++)
- News Story: Mozilla Corp board member leaks the truth about Firefox memory loss (Web Browsers)
- memory leak problem... (C)
- Windows XP keeps restarting since a new video card (Windows NT / 2000 / XP)
- Memory Loss?Tom Coyote Tweak (Windows tips 'n' tweaks)
- XFX mobo -> cd drive connectivity problem (Motherboards, CPUs and RAM)
Other Threads in the Java Forum
- Previous Thread: Java Applet viewer blinks everytime the init method is used.
- Next Thread: Creating Java interface to my c++ program
| Thread Tools | Search this Thread |
6 @param actuate affinetransform android api applet application arc array automation balls binary bluetooth bold business c++ class client code codesnippet collections color compare component coordinates database defaultmethod detection doctype dragging ebook eclipse educational error file fractal froglogic game givemetehcodez graphics gridlayout gui guitesting helpwithhomework html ide ideas image ingres intersect invokingapacheantprogrammatically j2me java java.xls javaexcel javaprojects jni jpanel jtextarea julia keytool keyword linux list map method methods mobile mysql netbeans nextline object parameter php pong problem producer program project projectideas recursive replaysolutions rim scanner sell server set size sms sql sun swing swt terminal threads tree web websites windows





