simple GUI calculator

trying to get this simple GUI calculator to work

Page 1 of 1

1 Replies - 14802 Views - Last Post: 14 December 2008 - 04:24 PM Rate Topic: -----

#1 volkman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 31-October 08

simple GUI calculator

Post icon  Posted 14 December 2008 - 04:08 PM

Help
trying to get second number text box to work cant figure it out some help would be apreicated
also it would needs to have more then a single digit number cant seem to get that to work either and lastly the decimal point function does work been trying to figure this for awhile now here is my code and I hope I posted correctly. If not I apologized for it.


  import javax.swing.*;
	import java.awt.*;
import java.awt.event.*;



	public  class FinalProject  extends JFrame 
	{
	
		private static final int WIDTH = 500;
		private static final int HEIGHT = 250;
		 private static final Font BIGGER_FONT = 
			 new Font("monspaced", Font.PLAIN, 20);
		 String value,A;
		 
		 
		private JTextField num1TF,operTF,num2TF,answerTF;
		private JButton exitB,calculateB,clearB,dotB,zeroB,oneB,twoB,threeB,fourB,fiveB,sixB,sevenB,eightB,nineB,oper1B,oper2B,oper3B,oper4B;
		private CalculateButtonHandler cbHandler;
		private clearButtonHandler clearHandler;
		private dotButtonHandler dotHandler;
		private zeroButtonHandler zeroHandler;
		private oneButtonHandler oneHandler;
		private twoButtonHandler twoHandler;
		private threeButtonHandler threeHandler;
		private fourButtonHandler fourHandler;
		private fiveButtonHandler fiveHandler;
		private sixButtonHandler sixHandler;
		private sevenButtonHandler sevenHandler;
		private eightButtonHandler eightHandler;
		private nineButtonHandler nineHandler;
		private oper1ButtonHandler oper1Handler;
		private oper2ButtonHandler oper2Handler;
		private oper3ButtonHandler oper3Handler;
		private oper4ButtonHandler oper4Handler;
		private ExitButtonHandler ebHandler;
		
		

		public FinalProject()
		{
			setTitle("Calculator");
	
			
			num1TF = new JTextField(2);
			operTF = new JTextField(2);
			num2TF = new JTextField(2);
			answerTF = new  JTextField(2);
			
			
			
			sevenB = new JButton("7");
			sevenHandler = new sevenButtonHandler();
			sevenB.addActionListener(sevenHandler);
			sevenB.setFont(BIGGER_FONT);
			
			eightB = new JButton("8");
			eightHandler = new eightButtonHandler();
			eightB.addActionListener(eightHandler);
			eightB.setFont(BIGGER_FONT);
			
			nineB = new JButton("9");
			nineHandler = new nineButtonHandler();
			nineB.addActionListener(nineHandler);
			nineB.setFont(BIGGER_FONT);
			
			oper4B = new JButton("/");
			oper4Handler = new oper4ButtonHandler();
			oper4B.addActionListener(oper4Handler);
			oper4B.setFont(BIGGER_FONT);
			
			fourB = new JButton("4");
			fourHandler = new fourButtonHandler();
			fourB.addActionListener(fourHandler);
			fourB.setFont(BIGGER_FONT);
			
			fiveB = new JButton("5");
			fiveHandler = new fiveButtonHandler();
			fiveB.addActionListener(fiveHandler);
			fiveB.setFont(BIGGER_FONT);
			
			sixB = new JButton("6");
			sixHandler = new sixButtonHandler();
			sixB.addActionListener(sixHandler);
			sixB.setFont(BIGGER_FONT);
			
			oper3B = new JButton("*");
			oper3Handler = new oper3ButtonHandler();
			oper3B.addActionListener(oper3Handler);
			oper3B.setFont(BIGGER_FONT);
			
			oneB = new JButton("1");
			oneHandler = new oneButtonHandler();
			oneB.addActionListener(oneHandler);
			oneB.setFont(BIGGER_FONT);
			
			twoB = new JButton("2");
			twoHandler = new twoButtonHandler();
			twoB.addActionListener(twoHandler);
			twoB.setFont(BIGGER_FONT);
			
			threeB = new JButton("3");
			threeHandler = new threeButtonHandler();
			threeB.addActionListener(threeHandler);
			threeB.setFont(BIGGER_FONT);
			
			oper2B = new JButton("-");
			oper2Handler = new oper2ButtonHandler();
			oper2B.addActionListener(oper2Handler);
			oper2B.setFont(BIGGER_FONT);
			
			clearB = new JButton("C");
			clearHandler = new clearButtonHandler();
			clearB.addActionListener(clearHandler);
			clearB.setFont(BIGGER_FONT);
			
			zeroB = new JButton("0");
			zeroHandler = new zeroButtonHandler();
			zeroB.addActionListener(zeroHandler);
			zeroB.setFont(BIGGER_FONT);
			
			dotB = new JButton(".");
			dotHandler = new dotButtonHandler();
			dotB.addActionListener(dotHandler);
			dotB.setFont(BIGGER_FONT);
			
			oper1B = new JButton("+");
			oper1Handler = new oper1ButtonHandler();
			oper1B.addActionListener(oper1Handler);
			oper1B.setFont(BIGGER_FONT);
			
			calculateB = new JButton("=");
			cbHandler = new CalculateButtonHandler();
			calculateB.addActionListener(cbHandler);
			calculateB.setFont(BIGGER_FONT);
			
			exitB = new JButton("exit");
			ebHandler = new ExitButtonHandler();
			exitB.addActionListener(ebHandler);
			exitB.setFont(BIGGER_FONT);
			
			
			Container pane = getContentPane();
			pane.setLayout(new GridLayout(6,1));
			
			pane.add(num1TF);
			pane.add(operTF);
			pane.add(num2TF);
			pane.add(answerTF);
			pane.add(sevenB);
			pane.add(eightB);
			pane.add(nineB);
			pane.add(oper4B);
			pane.add(fourB);
			pane.add(fiveB);
			pane.add(sixB);
			pane.add(oper3B);
			pane.add(oneB);
			pane.add(twoB);
			pane.add(threeB);
			pane.add(oper2B);
			pane.add(clearB);
			pane.add(zeroB);
			pane.add(dotB);
			pane.add(oper1B);
			pane.add(calculateB);
			pane.add(exitB);
			
			setSize(WIDTH,HEIGHT);
			setDefaultCloseOperation(EXIT_ON_CLOSE);
			setVisible(true);
		}

		private class zeroButtonHandler implements ActionListener
		{
			public void actionPerformed(ActionEvent e)
			{
				value = "0";
				num1TF.setText(value);
			}
		}
		
		private class oneButtonHandler implements ActionListener
		{
			public void actionPerformed(ActionEvent e)
			{
				value = "1";
				num1TF.setText(value);
			}
		}
	private class twoButtonHandler implements ActionListener
			{
				public void actionPerformed(ActionEvent e)
					{
					value = "2";
					num1TF.setText(value);	
					}
				
				
			}
		private class threeButtonHandler implements ActionListener
	{
				public void actionPerformed(ActionEvent e)
			{
						value = "3";
				num1TF.setText(value);
				}		
	}
		private class fourButtonHandler implements ActionListener
		{
			public void actionPerformed(ActionEvent e)
		{
							value = "4";
						num1TF.setText(value);
						
				}
		}
		private class fiveButtonHandler implements ActionListener
		{
		public void actionPerformed(ActionEvent e)
			{
						value = "5";
						num1TF.setText(value);
							
			}
		}
		private class sixButtonHandler implements ActionListener
		{
			public void actionPerformed(ActionEvent e)
				{
							value = "6";
						num1TF.setText(value);
							
				}
		}
		private class sevenButtonHandler implements ActionListener
		{
			public void actionPerformed(ActionEvent e)
		{
				value = "7";
			num1TF.setText(value);
				
			}	
		}
		private class eightButtonHandler implements ActionListener
		{
				public void actionPerformed(ActionEvent e)
				{
					value= "8";
					num1TF.setText(value);
				}
			}
			
	private class nineButtonHandler implements ActionListener
		{
				public void actionPerformed(ActionEvent e)
				{
					value = "9";
					num1TF.setText(value);
					
				}
			}
		private class clearButtonHandler implements ActionListener
			{
				public void actionPerformed(ActionEvent e)
					{
								value = "";
							num1TF.setText(value);
							operTF.setText(value);
							num2TF.setText(value);
							answerTF.setText(value);
				}
		}
		private class dotButtonHandler implements ActionListener
		{
			public void actionPerformed(ActionEvent e)
				{
					value+=".";
					num1TF.setText(value);
				}
			
		}
		private class oper1ButtonHandler implements ActionListener
	{
			public void actionPerformed(ActionEvent e)
			{
				String plus = "+";
			
				operTF.setText( plus);
			}
		}
		private class oper2ButtonHandler implements ActionListener
	{
			public void actionPerformed(ActionEvent e)
			{
				String minus = "-";
			
				operTF.setText( minus);
			}
		}
		private class oper3ButtonHandler implements ActionListener
		{
			public void actionPerformed(ActionEvent e)
			{
				String times = "*";
			
				operTF.setText( times);
			}
		}
		private class oper4ButtonHandler implements ActionListener
		{
			public void actionPerformed(ActionEvent e)
			{
				String div= "/";
			
				operTF.setText( div);
			}
		}
		private class CalculateButtonHandler implements ActionListener
		{
			public void actionPerformed(ActionEvent e)
			{
				double x,z,a =0.0;
				String y;
				x = Double.parseDouble(num1TF.getText());
				y = operTF.getText();
			z = Double.parseDouble(num2TF.getText());
				
				if (y.equals("+"))
				{
					a = x + z;
					answerTF.setText("= "+ a);
			}
				if (y.equals("-"))
			{
					a = x - z;
					answerTF.setText("= "+ a);
				}
				if (y.equals("*"))
				{
					a = x * z;
					answerTF.setText("= "+ a);
				}
				if(y.equals("/"))
				{  
					a = x / z;
					if (x == 0 || z == 0)
					{
					
						answerTF.setText("ERROR!!!!!");
					}
					else
					{
						answerTF.setText("" + a);
					}
				}
				
			}
		}
		
		private class ExitButtonHandler implements ActionListener
		{
			public void actionPerformed( ActionEvent e)
			{
				System.exit(0);
			}
		}
		
		public static void main(String[] args) 
		{
			FinalProject calculator = new FinalProject ();
			 {

	}
			 }}



Is This A Good Question/Topic? 0
  • +

Replies To: simple GUI calculator

#2 Mavirick  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 72
  • Joined: 18-February 08

Re: simple GUI calculator

Posted 14 December 2008 - 04:24 PM

Even besides the inefficiency (you could just have one ActionListener for all the buttons which calls for the name of the button last pressed and interprets what value to pass from there) the bulk of your problem lies in what you call your "Button Handlers." Every time a number is pressed, the text within JTextField "num1TF" is changed to that value. What you need to do instead is append that String.

Try this:
private class /*Whatever Number*/ButtonHandler implements ActionListener
		{
			public void actionPerformed(ActionEvent e)
			{
				value = /*Whatever Number in String form*/;
				num1TF.setText(num1TF.getText()+value);
			}
		}


That will simply add String value onto whatever is already in the JTextField.

Problem is, you've no way to determine whether to set the text in the first field or the third field, i.e. whether the user has already entered an operation (+,-,*,etc.). This could be done by adding to your Button Handlers/ActionListeners something that determines whether JTextField operTF is empty or not and changing the text in num1TF or num2TF based on that.

Good luck!

-Mav

This post has been edited by Mavirick: 14 December 2008 - 04:26 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1