Results 1 to 3 of 3
  1. #1
    all eyes is offline Member
    Join Date
    Mar 2008
    Posts
    2
    Rep Power
    0

    Default how to display a sum of all previously pressed numbers in JTextField?

    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.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Calc3 {
        private JTextField d1;
    
        public static void main (String[] args) {
            Calc3 g = new Calc3();
            g.go();
        }
    
        public void go() {
            JFrame frame = new JFrame("Simple");
            // Stick with swing/lightweight components:
            JPanel mainPanel = new JPanel();
            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(300,100);
            frame.setLocation(200,200);
            frame.setVisible(true);
        }
    
        public class AddListener implements ActionListener {
            int sum = 0;
    
            public void actionPerformed(ActionEvent ev) {
                String x = d1.getText();
                int n = Integer.parseInt(x);
                System.out.println("adding " + n + " to " + sum +
                                   " = " + (n + sum));
                sum += n;
                d1.setText("");
                d1.requestFocus();
    
                // You can use a JLabel in the south section
                // to show results.
                //d1.setText(Integer.toString(sum));
            }
        }
    }

  3. #3
    all eyes is offline Member
    Join Date
    Mar 2008
    Posts
    2
    Rep Power
    0

Similar Threads

  1. How to display numbers with leading zeros
    By Java Tip in forum java.lang
    Replies: 1
    Last Post: 06-14-2008, 06:36 PM
  2. when muse pressed the background change
    By pcman in forum Java Applets
    Replies: 1
    Last Post: 03-17-2008, 11:51 PM
  3. key pressed event
    By kavithas in forum New To Java
    Replies: 7
    Last Post: 12-10-2007, 02:01 PM
  4. returning to a previously saved view
    By gojava in forum Advanced Java
    Replies: 0
    Last Post: 11-09-2007, 05:11 PM
  5. How to display numbers with leading zeros
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 09:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •