Results 1 to 3 of 3
- 04-01-2012, 06:01 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 2
- Rep Power
- 0
Buttons for my textfields (calculator)
Hey,
First I'd like to start off and say I am quite new to java programming but find it very interesting. Currently I am trying to create a calculator, not any calculator because I plan it to have a few options which normal calculators doesn't have, such as change background color etc, basic stuff probably.
So far I have created two textfields in which I can type two different values and then I got a few buttons which will calculate what these two values equals if I choose +, -, * or /. What I would want it to be like is that above these textfields I have 9 buttons (1-9). When I select a textfield and press for example 1, 2, 3 I want 123 to be the value of that textfield and if I select the other textfield and press 7, 6, 5 I want 765 to be the value of that other textfield.
Any idea how I can make this happen?
Currently my ActionPerformed receives this info when I type something in the different textfields:
Java Code:public void actionPerformed(ActionEvent e) { x = Integer.parseInt(tf1.getText()); y = Integer.parseInt(tf2.getText()); if (e.getSource() == bplus) { summa = x + y; } if (e.getSource() == bminus) { summa = x - y; } if (e.getSource() == bganger) { summa = x * y; } if (e.getSource() == bdela) { summa = x / y; } lbl2.setText("Summan är " + summa);
- 04-01-2012, 06:15 PM #2
Member
- Join Date
- Sep 2010
- Posts
- 84
- Rep Power
- 0
Re: Buttons for my textfields (calculator)
off the top of my head when one of the buttons is pressed (1-9), u need to get the string representation of that (1-9) number and then when pressed again, u need to add to that string. Then when you want to multiple, you would need to change back to int/double etc.
(I"m a newbie, so not sure if that's the best way)
- 04-01-2012, 06:19 PM #3
Member
- Join Date
- Apr 2012
- Posts
- 2
- Rep Power
- 0
Re: Buttons for my textfields (calculator)
Thanks for your answer.
Actually I'm not quite sure if this is the best way to do it. What I don't understand is how I am supposed to get the buttons value inserted to the textfields. Maybe I shouldn't use these kind of textfields? Maybe it's better to put these values somewhere else?
Similar Threads
-
textfields - cursor
By newbie123 in forum AWT / SwingReplies: 6Last Post: 09-29-2011, 07:39 PM -
applet and textfields
By poajavaweb in forum Java AppletsReplies: 4Last Post: 01-28-2011, 09:56 PM -
adding textfields
By themanepalli in forum Java AppletsReplies: 1Last Post: 12-28-2010, 04:56 AM -
Buttons and TextFields
By Xystus777 in forum New To JavaReplies: 27Last Post: 03-17-2009, 02:26 AM -
How can I place Buttons and TExtfields within an Applet window?
By sergioTOOL in forum JCreatorReplies: 0Last Post: 02-12-2009, 12:12 AM
Bookmarks