Results 1 to 3 of 3
  1. #1
    NewJohn is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default 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);
    Thankful for your help!

  2. #2
    Danieldcc is offline Member
    Join Date
    Sep 2010
    Posts
    83
    Rep Power
    0

    Default 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)

  3. #3
    NewJohn is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default 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

  1. textfields - cursor
    By newbie123 in forum AWT / Swing
    Replies: 6
    Last Post: 09-29-2011, 07:39 PM
  2. applet and textfields
    By poajavaweb in forum Java Applets
    Replies: 4
    Last Post: 01-28-2011, 09:56 PM
  3. adding textfields
    By themanepalli in forum Java Applets
    Replies: 1
    Last Post: 12-28-2010, 04:56 AM
  4. Buttons and TextFields
    By Xystus777 in forum New To Java
    Replies: 27
    Last Post: 03-17-2009, 02:26 AM
  5. Replies: 0
    Last Post: 02-12-2009, 12:12 AM

Posting Permissions

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