Results 1 to 3 of 3
- 03-30-2008, 06:56 PM #1
Member
- Join Date
- Mar 2008
- Posts
- 2
- Rep Power
- 0
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.
- 03-30-2008, 08:10 PM #2
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)); } } }
- 03-30-2008, 08:38 PM #3
Member
- Join Date
- Mar 2008
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
How to display numbers with leading zeros
By Java Tip in forum java.langReplies: 1Last Post: 06-14-2008, 06:36 PM -
when muse pressed the background change
By pcman in forum Java AppletsReplies: 1Last Post: 03-17-2008, 11:51 PM -
key pressed event
By kavithas in forum New To JavaReplies: 7Last Post: 12-10-2007, 02:01 PM -
returning to a previously saved view
By gojava in forum Advanced JavaReplies: 0Last Post: 11-09-2007, 05:11 PM -
How to display numbers with leading zeros
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:34 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks