hi all,
i am using here JTextField to get sum of all previously pressed numbers in one button called (+) and so on.
there is only one button take care of addition and displaying the result in the same JTextFeild, like :
1-press any number, called it 1.
2-press the (+) button to add previous number.
3-press anthor number, called it 5.
4-again press the (+) button to add two numbers(1+5)and show the answer(6) .
5-again press anthor number called it 4.
6-press the (+) button again ,to add previous result(6) to current number(4)and show the answer that is(10) and so on...
here is the code:
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.io.*;
public class Calc2 {
private JTextField d1;
private JFrame frame;
private int a,s,sum;
public static void main (String[] args) {
Calc2 g = new Calc2();
g.go();
} // close main
public void go() {
frame = new JFrame("Simple");
Panel mainPanel = new Panel();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
Font bigFont = new Font("sanserif",Font.BOLD,24);
d1 = new JTextField(10);
JButton b = new JButton("+");
b.addActionListener(new AddListener());
mainPanel.add(d1);
mainPanel.add(b);
frame.getContentPane().add(BorderLayout.CENTER,mai nPanel);
frame.setSize(500,600);
frame.setVisible(true);
}//close go()
public class AddListener implements ActionListener {
public void actionPerformed(ActionEvent ev) {
String x = d1.getText();
s = Integer.parseInt(x);
d1.setText("");
d1.requestFocus();
String z = d1.getText();
a = Integer.parseInt(z);
sum = a + s;
//Now how to keep track of a sum variable, and a currentValue variable?.
d1.setText(Integer.toString(sum));
}
}//close inner
}//close class
any body can help me Keep track of previous sum, and current value. When i hit the button, add the two.