Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: calculator GUI needs to compute

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default calculator GUI needs to compute

    import java.awt.*;
    import java.awt.Color.*;
    import java.awt.TextField.*;
    import java.awt.event.*;
    public class Calculator extends WindowAdapter
    {
    	//declaraion of variable types
    	Frame f;
    	TextField tf;
     
    	Button  b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16;
    	Panel west, center;
     
    	public void makeCALC()
    	{
    		f = new Frame("Calculate it");
    		tf = new TextField("0.0");
     
    //assigning values to buttons
    		b1 = new Button("7");
    		b2 = new Button("8");
    		b3 = new Button("9");
    		b4 = new Button("/");
    		b5 = new Button("4");
    		b6 = new Button("5");
    		b7 = new Button("6");
    		b8 = new Button("*");
    		b9 = new Button("1");
    		b10 = new Button("2");
    		b11 = new Button("3");
    		b12 = new Button("-");
    		b13 = new Button("0");
    		b14 = new Button("=");
    		b15 = new Button(".");
    		b16 = new Button("+");
    //setting up alignment and layout of grid
    		west = new Panel();
    		center = new Panel();
    		center.setLayout(new GridLayout(4,4));
    		f.add(tf,BorderLayout.NORTH);
    		f.add(west,BorderLayout.WEST);
    		f.add(center,BorderLayout.CENTER);
     
    		center.add(b1);
    		center.add(b2);
    		center.add(b3);
    		center.add(b4);
    		center.add(b5);
    		center.add(b6);
    		center.add(b7);
    		center.add(b8);
    		center.add(b9);
    		center.add(b10);
    		center.add(b11);
    		center.add(b12);
    		center.add(b13);
    		center.add(b14);
    		center.add(b15);
    		center.add(b16);
    		f.setSize(500,500);
    		f.setVisible(true);
     
    //formating of font and color of numbers
    		Font font = new Font("Verdana", Font.BOLD,40);
    		f.setFont(font);
    		b1.setForeground(Color.BLUE);
    		b2.setForeground(Color.YELLOW);
    		b3.setForeground(Color.DARK_GRAY);
    		b4.setForeground(Color.RED);
    		b5.setForeground(Color.GREEN);
    		b6.setForeground(Color.CYAN);
    		b7.setForeground(Color.LIGHT_GRAY);
    		b8.setForeground(Color.ORANGE);
    		b9.setForeground(Color.RED);
    		b10.setForeground(Color.PINK);
    		b11.setForeground(Color.GRAY);
    		b12.setForeground(Color.MAGENTA);
    		b13.setForeground(Color.WHITE);
    		b14.setForeground(Color.YELLOW);
    		b15.setForeground(Color.ORANGE);
    		b16.setForeground(Color.PINK);
     
    //method caller for buttons and frame
    		f.addWindowListener(this);
    		b1.addActionListener(new MyButtonHandler());
    		b2.addActionListener(new MyButtonHandler());
    		b3.addActionListener(new MyButtonHandler());
    		b4.addActionListener(new MyButtonHandler());
    		b5.addActionListener(new MyButtonHandler());
    		b6.addActionListener(new MyButtonHandler());
    		b7.addActionListener(new MyButtonHandler());
    		b8.addActionListener(new MyButtonHandler());
    		b9.addActionListener(new MyButtonHandler());
    		b10.addActionListener(new MyButtonHandler());
    		b11.addActionListener(new MyButtonHandler());
    		b12.addActionListener(new MyButtonHandler());
    		b13.addActionListener(new MyButtonHandler());
    		b14.addActionListener(new MyButtonHandler());
    		b15.addActionListener(new MyButtonHandler());
    		b16.addActionListener(new MyButtonHandler());
     
     
    }
    //calling method to allow window to close
     
    	public void windowClosing(WindowEvent we)
    	{
    		System.exit(0);
    }
    	public class MyButtonHandler implements ActionListener
    	{
    		public void actionPerformed(ActionEvent ae)
    		{
    //method to test values of label stored in button
    			String label = ae.getActionCommand();
     
    			if(label == "7")
    			{
    				tf.setText("7");
    			}
    			else if(label == "8")
    			{
    				tf.setText("8");
     
    			}
    			if(label == "9")
    			{
    				tf.setText("9");
    			}
    			else if(label == "/")
    			{
    				tf.setText("/");
    			}
    			if(label == "4")
    			{
    				tf.setText("4");
    			}
    			else if(label == "5")
    			{
    			tf.setText("5");
     
    			}
    			if(label == "6")
    			{
    			tf.setText("6");
    			}
    			else if(label == "*")
    			{
    			tf.setText("*");
     
    			}
    			else if(label == "1")
    			{
    				tf.setText("1");
    			}
    			else if(label == "2")
    			{
    				tf.setText("2");
    			}
    			else if(label == "3")
    			{
    				tf.setText("3");
    			}
    			else if(label == "-")
    			{
    				tf.setText("-");
    			}
    			else if(label == "0")
    			{
    				tf.setText("0");
    			}
    			else if(label == "=")
    			{
    				tf.setText("=");
    			}
    			else if(label == ".")
    			{
    				tf.setText(".");
    			}
    			else if(label == "+")
    			{
    				tf.setText("+");
    			}
     
    			else if(label == "Clear")
    			{
    				tf.setText("  ");
    	}
    		}
    		}//end makeGUI()
    		public static void main(String args[])
    		{
    			Calculator theNew = new Calculator();
    			theNew.makeCALC();
     
    	}
    Last edited by helloworld922; May 4th, 2010 at 11:18 AM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: calculator GUI needs to compute

    What is it you need help with?

  3. #3
    Junior Member
    Join Date
    Apr 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: calculator GUI needs to compute

    i need the calculator to compute and do normal calculations like 3+4=7

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: calculator GUI needs to compute

    There are multiple ways to do this.

    Here's one implementation of a simple Java calculator parser based on the Shunting Yard algorithm I made a while back: Java Calculator. This algorithm is extremely easy to implement and allows you to parse with order of operations.

    If you don't want order of operations, the parsing gets even easier:

    1. read in a token (hopefully a number, otherwise syntax error). This is operand 1
    2. read in another token (hopefully an operator, otherwise syntax error)
    3. read in another token (hopefully a number, otherwise syntax error). set this to operand 2
    4. Perform operand1 = operand1 {operator} operand2
    5. repead steps 2-4 until there are no more tokens, or there is a syntax error
    6. The answer is stored in operand1

Similar Threads

  1. Compute the frequency count and big-oh notation for a certain code segment
    By maykel_trinidad in forum Java Theory & Questions
    Replies: 3
    Last Post: November 13th, 2009, 10:23 AM
  2. Calculator help.
    By Skinnyskinny in forum Java Theory & Questions
    Replies: 6
    Last Post: August 1st, 2009, 12:34 PM
  3. Help codiing calculator
    By FM010 in forum Exceptions
    Replies: 7
    Last Post: June 12th, 2009, 02:25 PM
  4. Calculator application using java
    By fabolous04 in forum Paid Java Projects
    Replies: 4
    Last Post: March 25th, 2009, 11:29 AM
  5. Problem of implementing mathematic logic in Java applet
    By AnithaBabu1 in forum Java Applets
    Replies: 0
    Last Post: August 15th, 2008, 11:42 PM

Tags for this Thread