Results 1 to 11 of 11

Thread: calculator

  1. #1
    rithish is offline Member
    Join Date
    Apr 2011
    Posts
    40
    Rep Power
    0

    Default calculator

    Java Code:
    import javax.swing.*;
    
    import java.awt.*;
    import java.awt.event.*;
    class calculator extends JFrame
    {
    	JPanel p1=new JPanel();
    	JTextField t1=new JTextField();
    	JTextField t2=new JTextField();
    	JTextField t3=new JTextField();
    	JButton b1=new JButton("+");
    	
    	public calculator()
    	{
    		
    		p1.add(t1);
    		p1.add(t2);
    		p1.add(t3);
    		p1.add(b1);
    		
    		
    		
    		b1.addActionListener(new ActionListener() {
    			
    			public void actionPerformed(ActionEvent ae)
    			{
    				
    				String value=t1.getText();
    				String value1=t2.getText();
    				
    				t3.setText()=value+value1;
    				
    				
    			}
    			
    			
    			
    		});
    		
    		
    	}		
    public static void main(String args[])
    {
    	
    	calculator frame=new calculator();
    	
    }
    		
    }


    hello actually i use three textboxes third textbox i want to display the output
    one and two textbox will get and display in output.but i have done only for additon beacuse its gettting error i dont know what to do ?????please help me sir

  2. #2
    rithish is offline Member
    Join Date
    Apr 2011
    Posts
    40
    Rep Power
    0

    Default

    i dont know how to get values from text1 and text2

  3. #3
    rithish is offline Member
    Join Date
    Apr 2011
    Posts
    40
    Rep Power
    0

    Default

    hello please help

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by rithish View Post
    i dont know how to get values from text1 and text2
    You're already getting values from the two JTextFields. What you're not doing is parsing them -- check out either Double.parseDouble(...) or Integer.parseInt(...) depending on what is being entered, or setting the text in the third properly -- you must pass a String into the setText(...) method, or it seems adding your JPanel p1 to the JFrame. Lot's of errors there. Have you gone through the tutorials? Also if you have errors in your code, please post the error messages themselves and indicate which line causes them.

  5. #5
    rithish is offline Member
    Join Date
    Apr 2011
    Posts
    40
    Rep Power
    0

    Default

    this is the error message

    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    The left-hand side of an assignment must be a variable
    at calculator.<init>(calculator.java:31)
    at calculator.main(calculator.java:45)

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by rithish View Post
    this is the error message

    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    The left-hand side of an assignment must be a variable
    at calculator.<init>(calculator.java:31)
    at calculator.main(calculator.java:45)
    Good, but which line? Probably this one:

    Java Code:
    t3.setText()=value+value1;
    You've got a method on the left and an equation on the right -- makes no sense.

    You must do the addition and put it into a variable, then change that into a String -- String.valueOf(myNumberVariable) will work for this, and then call setText passing this String into the method. Again, please read the tutorials for more on this.

  7. #7
    rithish is offline Member
    Join Date
    Apr 2011
    Posts
    40
    Rep Power
    0

    Default

    tutorails mean??? where is it?? iam reading from ebooks sir

  8. #8
    rithish is offline Member
    Join Date
    Apr 2011
    Posts
    40
    Rep Power
    0

    Default

    i want link tutorials from this one

  9. #9
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Trail: Graphical User Interfaces (The Java™ Tutorials)

    EDIT: Actually, you don't seem to know how to use methods so I suggest reading this instead.

  10. #10
    rithish is offline Member
    Join Date
    Apr 2011
    Posts
    40
    Rep Power
    0

    Default

    sir i know to parse but how can i do it please show me some examples like this similiar to this

  11. #11
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Java Code:
    t3.setText()=value+value1;
    That is not valid Java syntax.
    What you need is to do Double.parseDouble(value) and value1 and add the returned values together to set it in "t3".

Similar Threads

  1. Help with AWT CALCULATOR
    By Megan Dosnueve in forum AWT / Swing
    Replies: 2
    Last Post: 04-04-2011, 06:49 PM
  2. Calculator
    By Moshe22 in forum New To Java
    Replies: 8
    Last Post: 01-17-2011, 06:29 AM
  3. Help in a calculator
    By Ayannie in forum New To Java
    Replies: 6
    Last Post: 01-04-2011, 09:21 PM
  4. help with calculator
    By kalibballer in forum New To Java
    Replies: 8
    Last Post: 04-01-2009, 01:57 PM
  5. Calculator help.
    By madkidd02 in forum New To Java
    Replies: 2
    Last Post: 10-25-2008, 08:42 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
  •