Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-30-2008, 07:56 PM
Member
 
Join Date: Mar 2008
Posts: 2
all eyes is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-30-2008, 09:10 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
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)); } } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 03-30-2008, 09:38 PM
Member
 
Join Date: Mar 2008
Posts: 2
all eyes is on a distinguished road
thank you for replying
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to display numbers with leading zeros Java Tip java.lang 1 06-14-2008 07:36 PM
when muse pressed the background change pcman Java Applets 1 03-18-2008 12:51 AM
key pressed event kavithas New To Java 7 12-10-2007 03:01 PM
returning to a previously saved view gojava Advanced Java 0 11-09-2007 06:11 PM
How to display numbers with leading zeros JavaBean Java Tips 0 10-04-2007 10:34 PM


All times are GMT +3. The time now is 02:24 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org