display a sum of all previously pressed numbers in JTextField?

Discussion in 'Software' started by eyeball, Mar 28, 2008.

  1. eyeball

    eyeball Private E-2

    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_CLOSE);
    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,mainPanel);
    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.
     
  2. PC-XT

    PC-XT Master Sergeant

    It looks like you could use sum+=s instead of sum=a+s, unless you want to keep track of the previous sum, in which case you could use a=sum;sum+=s; to put the previous sum in a. String z=d1.getText() will always get "" since you set it to that value just before that, so a will always be 0, and the sum will not update. You may want to use a different text box for the sum. Or for just one box, use sum to keep track of the other value, and forget about z and a.
     
    Last edited: Mar 31, 2008

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds