473,512 Members | 14,457 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

3 New Member
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.
Mar 29 '08 #1
2 2396
Ganon11
3,652 Recognized Expert Specialist
Try using a Stack. If the stack is empty and a number is pressed, push that number onto the stack. If the + button is pressed and there is a number o the stack, pop it and use it as one of the operands. Then push the sum back onto the stack.
Mar 29 '08 #2
all eyes
3 New Member
Try using a Stack. If the stack is empty and a number is pressed, push that number onto the stack. If the + button is pressed and there is a number o the stack, pop it and use it as one of the operands. Then push the sum back onto the stack.
i am sorry for being late,if you can explain it whit little code segment please, will appreciate
Mar 30 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
3687
by: Alexander Mazo | last post by:
Hi! I'm new in JAVA. I'm wont change "Home" and "End", but nothing receive. public class RLTextField { public static String s = new String(); public static void main(String args) { JFrame...
4
3039
by: active | last post by:
I can use Control.ModifierKeys to determine if a modifying key is pressed when executing MouseDown event, but how can I determine if an "M" was pressed?? Possible? Can't find a clue in...
3
5229
by: hzgt9b | last post by:
Using VS2005, VB.NET, I am developing a windows app that has a DataGridView. I want to enable the display of a context menu on this DataGridView only when a specific set of keys is also pressed...
0
2029
JoeMac3313
by: JoeMac3313 | last post by:
The question is how can I update the JTextField in Class CreditDisplayField when I hit the button in class CreditButton? I wrote in a test printing to console to make sure the "logic.addCredits();"...
6
7333
by: AhmedGY | last post by:
Hi, I'm new to C#, and doing a simple calculator application, it's completed now and calculates well, i wanna provide the interface with hotkeys so when the user press a number lets say "1" on the...
1
2800
by: ahmed222too | last post by:
the jtextfield donot accept Arabic language font when i run this code jtextfield.settext="ذهب الولد إلى المدرسة"; the arabic font change into another font in the jtextfield
4
1968
by: ahmed222too | last post by:
when i read data from file by code into jtextfield the lines of the file apprear in Concatenation in the jtextfield (without Enters between lines) i want to transfere the content of the file to the...
3
10731
by: Kid Programmer | last post by:
Hello guys. I was wondering if it is possible to make a JTextField read only. In a program I am writing I don't want the user to be able to edit the text in a JTextField.
5
7435
by: milk242 | last post by:
Hi everyone I'm trying to change what shows up in the contentpane depending on what button you press. Say you press a button labeled "Customer", it would add textfields to the content pane. I...
0
7153
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7371
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7093
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5676
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5077
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1583
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
452
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.