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 2 of 2

Thread: hi: i need help with this programm, i need it to make calculations as a normal calc

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

    Default hi: i need help with this programm, i need it to make calculations as a normal calc

    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();
     
    	}
    }


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: hi: i need help with this programm, i need it to make calculations as a normal ca

    Firstly, please post in the correct forum. Secondly, please add code tags around your code.

    Post a description with your code. I am unsure what you are looking for.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

Similar Threads

  1. Trying to make a bot
    By ighor10 in forum Java Theory & Questions
    Replies: 1
    Last Post: June 11th, 2010, 02:07 PM
  2. make forum with EJB??!!
    By element_sole in forum Enterprise JavaBeans
    Replies: 3
    Last Post: February 12th, 2010, 01:18 PM
  3. How to make it???
    By Subhasis Banerjee in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: October 29th, 2009, 02:49 PM
  4. GPA calc code... HELP
    By ber1023 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 13th, 2009, 11:20 PM
  5. Java algorithm for bank program to connect database
    By araujo3rd in forum Algorithms & Recursion
    Replies: 1
    Last Post: December 10th, 2008, 12:34 PM