How to develop calculator using Swing

Learn development of Calculator

In this tutorial we will learn development of calculator GUI. For simplicity we use FlowLayout for adding buttons in our calculator.

Let's see now code to create calculator gui.


Download Icon Image Here
Download Source Code

Program

/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class calculator
 {
 JFrame f;
 JTextField jt;
 handler h;
 JPanel jf,jf1;
 JButton b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,badd,bsub,bmul,bsqrt,bdiv,bsign,bqrt,bsquare,bequal,bclr,brshift,blshift,brpshift,band,bor,bxor,bnot,bbinary,bocta,bhexa;
 ImageIcon icon;
 calculator()
  {
    f=new JFrame("Calculator version 1.0");
	Font font1 = new Font("SansSerif", Font.BOLD, 13);
	jf=new JPanel();
	jf1=new JPanel();
	jt=new JTextField(28);
	jt.setHorizontalAlignment(JTextField.RIGHT);
	jt.setPreferredSize(new Dimension(300,40));
	jt.setFont(font1);
	h=new handler(jt);
	b0=new JButton("0");
	b0.addActionListener(h);
	b1=new JButton("1");
	b1.addActionListener(h);
	b2=new JButton("2");
	b2.addActionListener(h);
	b3=new JButton("3");
	b3.addActionListener(h);
	b4=new JButton("4");
	b4.addActionListener(h);
	b5=new JButton("5");
	b5.addActionListener(h);
	b6=new JButton("6");
	b6.addActionListener(h);
	b7=new JButton("7");
	b7.addActionListener(h);
	b8=new JButton("8");
	b8.addActionListener(h);
	b9=new JButton("9");
	b9.addActionListener(h);
	badd=new JButton("+");
	badd.addActionListener(h);
	bsub=new JButton("-");
	bsub.addActionListener(h);
	bmul=new JButton("*");
	bmul.addActionListener(h);
	bsqrt=new JButton("\u221A");
	bsqrt.addActionListener(h);
	bdiv=new JButton("/");
	bdiv.addActionListener(h);
	bsign=new JButton("+/-");
	bsign.addActionListener(h);
	bqrt=new JButton("\u221B");
	bqrt.addActionListener(h);
	bsquare=new JButton("x\u00b2");
	bsquare.addActionListener(h);
	bequal=new JButton("=");
	bequal.addActionListener(h);
	bclr=new JButton("CLR");
	bclr.addActionListener(h);
	brshift=new JButton(">>");
	brshift.addActionListener(h);
	blshift=new JButton("<<");
	blshift.addActionListener(h);
	brpshift=new JButton(">>>");
	brpshift.addActionListener(h);
	band=new JButton("&");
	band.addActionListener(h);
	bor=new JButton("|");
	bor.addActionListener(h);
	bnot=new JButton("~");
	bnot.addActionListener(h);
	bxor=new JButton("^");
	bxor.addActionListener(h);
	bbinary=new JButton("()\u2082");
	bbinary.addActionListener(h);
	bhexa=new JButton("()\u2081\u2086");
	bhexa.addActionListener(h);
	bocta=new JButton("()\u2088");
	bocta.addActionListener(h);
	f.setLayout(new FlowLayout());
	jf1.setLayout(new FlowLayout());
	jf.setLayout(new GridLayout(6,4,10,10));
	f.setResizable(false);
   icon=new ImageIcon("calculator.PNG");
	f.setIconImage(icon.getImage());
	
	createGUI();
  }
  void createGUI()
   {
    jf1.add(jt);
   
	jf.add(b0);
	jf.add(b1);
	jf.add(b2);
	jf.add(b3);
	jf.add(b4);
	jf.add(b5);
	jf.add(b6);
	jf.add(b7);
	jf.add(b8);
	jf.add(b9);
	jf.add(badd);
	jf.add(bsub);
	jf.add(bmul);
	jf.add(bdiv);
	jf.add(bsqrt);
	jf.add(bsign);
	jf.add(bqrt);
	jf.add(bsquare);
	jf.add(bequal);
	jf.add(bclr);
	jf.add(brshift);
	jf.add(blshift);
	jf.add(brpshift);
	jf.add(band);
	jf.add(bor);
	jf.add(bnot);
	jf.add(bxor);
	jf.add(bbinary);
	jf.add(bocta);
	jf.add(bhexa);
	f.setVisible(true);
	f.setSize(350,330);
	f.add(jf1);
	f.add(jf);
	f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	
   }
  public static void main(String arg[])
   {
     try 
	  {
	  //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
	   UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
	  }
	 catch(Exception e)
	   {
	      try{ UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}catch(Exception e1){}
	   }
     new calculator();
   }
 
 }
 class handler implements ActionListener
  { 
    JTextField jt;
	double op1,op2;
	boolean operation;
	boolean newnumber;
	String op;
    handler(JTextField t)
	 {
	   jt=t;
	   op1=0.0;
	   op2=0.0;
	   operation=false;
	   newnumber=false;
	 }
    public void actionPerformed(ActionEvent ae)
	 {
	   String com=ae.getActionCommand();
	   System.out.println("Pressed:"+com);
	   double result;
	   int o1,o2;
	   switch(com)
	    {
		case "0":if(newnumber==false)
		          jt.setText(jt.getText()+"0");
				  else 
				   {
				    jt.setText("0");
					newnumber=false;
				   }
		          break;
		case "1":if(newnumber==false)
		          jt.setText(jt.getText()+"1");
				  else 
				   {
				    jt.setText("1");
					newnumber=false;
				   }
		          break;
		case "2":if(newnumber==false)
		          jt.setText(jt.getText()+"2");
				  else 
				   {
				    jt.setText("2");
					newnumber=false;
				   }
		          break;
		case "3":if(newnumber==false)
		          jt.setText(jt.getText()+"3");
				  else 
				   {
				    jt.setText("3");
					newnumber=false;
				   }
		          break;
		case "4":if(newnumber==false)
		          jt.setText(jt.getText()+"4");
				  else 
				   {
				    jt.setText("4");
					newnumber=false;
				   }
		          break;
		case "5":if(newnumber==false)
		          jt.setText(jt.getText()+"5");
				  else 
				   {
				    jt.setText("5");
					newnumber=false;
				   }
		          break;
		case "6":if(newnumber==false)
		          jt.setText(jt.getText()+"6");
				  else 
				   {
				    jt.setText("6");
					newnumber=false;
				   }
		          break;
		case "7":if(newnumber==false)
		          jt.setText(jt.getText()+"7");
				  else 
				   {
				    jt.setText("7");
					newnumber=false;
				   }
		          break;
		case "8":if(newnumber==false)
		          jt.setText(jt.getText()+"8");
				  else 
				   {
				    jt.setText("8");
					newnumber=false;
				   }
		          break;
	    case "9":if(newnumber==false)
		          jt.setText(jt.getText()+"9");
				  else 
				   {
				    jt.setText("9");
					newnumber=false;
				   }
		          break;
		case "+": if(operation==false)
		            {
					 op1=Double.parseDouble(jt.getText());
				     jt.setText("");
					 operation=true;
					 op="+";
					}
				 else 
				   {
				      result=op1+Double.parseDouble(jt.getText());
				      if(result%1==0)								    
					   jt.setText(Integer.toString((int)result));
					  else 
					   jt.setText(Double.toString(result));
					   newnumber=true;
				   }
		          break;
		case "=": if(operation==true)
		            {
					  op2=Double.parseDouble(jt.getText());
					  
					  switch(op)
					    {
						  case "+": result=op1+op2;
						           if(result%1==0)								    
						            jt.setText(Integer.toString((int)result));
								  else 
								    jt.setText(Double.toString(result));
								  break;
						  case "-": result=op1-op2;
						           if(result%1==0)								    
						            jt.setText(Integer.toString((int)result));
								  else 
								       jt.setText(Double.toString(result));
								  break;
                          case "*": result=op1*op2;
						           if(result%1==0)								    
						            jt.setText(Integer.toString((int)result));
								  else 
								       jt.setText(Double.toString(result));
								  break;	
						  case "/": result=op1/op2;
						           if(result%1==0)								    
						            jt.setText(Integer.toString((int)result));
								  else 
								        jt.setText(Double.toString(result));
								  break;
						   case "<<": o1=(int)op1;o2=(int)op2;
						             result=((int)(o1))<<((int)(o2));
						         	 jt.setText(Integer.toString((int)result));
								     break;
						   case ">>": o1=(int)op1;o2=(int)op2;result=(o1>>o2);
						           								    
						            jt.setText(Integer.toString((int)result));
								  
								  break;	
						   case ">>>":o1=(int)op1;o2=(int)op2;result=(o1>>>o2);
						           								    
						            jt.setText(Integer.toString((int)result));
								  
								  break;
						  case "&": o1=(int)op1;o2=(int)op2;result=(o1&o2);
						         							    
						            jt.setText(Integer.toString((int)result));
								
								  break;
						 case ".": o1=(int)op1;o2=(int)op2;result=(o1>>o2);
						         							    
						            jt.setText(Integer.toString((int)result));
								
								  break;
						case "|": o1=(int)op1;o2=(int)op2;result=(o1|o2);
						         							    
						            jt.setText(Integer.toString((int)result));
								
								  break;
						case "^": o1=(int)op1;o2=(int)op2;result=(o1^o2);
						         							    
						            jt.setText(Integer.toString((int)result));
								
								  break;
							  
								  
						}
				      
					  operation=false;
					}
		         
		          break;
		case "-":if(operation==false)
		            {
					 op1=Double.parseDouble(jt.getText());
				     jt.setText("");
					 operation=true;
					 op="-";
					}
		          break;
		          
		case "*":if(operation==false)
		            {
					 op1=Double.parseDouble(jt.getText());
				     jt.setText("");
					 operation=true;
					 op="*";
					}
		          break;
		          
		case "/":if(operation==false)
		            {
					 op1=Double.parseDouble(jt.getText());
				     jt.setText("");
					 operation=true;
					 op="/";
					}
		          break;
		         
		case "CLR": operation=false;
		            jt.setText("");
		          break;
		case ">>":if(operation==false)
		            {
					 op1=Double.parseDouble(jt.getText());
				     jt.setText("");
					 operation=true;
					 op=">>";
					}
		          break;
		case "<<":if(operation==false)
		            {
					 op1=Double.parseDouble(jt.getText());
				     jt.setText("");
					 operation=true;
					 op="<<";
					}
		          break;
		case ">>>":if(operation==false)
		            {
					 op1=Double.parseDouble(jt.getText());
				     jt.setText("");
					 operation=true;
					 op=">>>";
					}
		          break;
		case "&":if(operation==false)
		            {
					 op1=Double.parseDouble(jt.getText());
				     jt.setText("");
					 operation=true;
					 op="&";
					}
		          break;
		case "|":if(operation==false)
		            {
					 op1=Double.parseDouble(jt.getText());
				     jt.setText("");
					 operation=true;
					 op="|";
					}
		          break;
		case "^":if(operation==false)
		            {
					 op1=Double.parseDouble(jt.getText());
				     jt.setText("");
					 operation=true;
					 op="^";
					}
		          break;
		case "+/-":if(operation==false)
		            {
					 op1=Double.parseDouble(jt.getText());
					 op1=op1*-1;
				     if(op1%1==0)								    
					  jt.setText(Integer.toString((int)op1));
				     else 
					  jt.setText(Double.toString(op1));
					 
					 
					}
		          break;	
		case "\u221A":if(operation==false)
		            {
					 op1=Double.parseDouble(jt.getText());
					 result=Math.sqrt(op1);
				     if(result%1==0)								    
					  jt.setText(Integer.toString((int)result));
				     else 
					  jt.setText(Double.toString(result));
					 
					 
					}
		          break;
		case "\u221b":if(operation==false)
		            {
					 op1=Double.parseDouble(jt.getText());
					result=Math.cbrt(op1);
				     if(result%1==0)								    
					  jt.setText(Integer.toString((int)result));
				     else 
					  jt.setText(Double.toString(result));
					 
					 
					}
		          break;
		case "x\u00b2":if(operation==false)
		            {
					 op1=Double.parseDouble(jt.getText());
					 op1=op1*op1;
				     if(op1%1==0)								    
					  jt.setText(Integer.toString((int)op1));
				     else 
					  jt.setText(Double.toString(op1));
					}
		          break;		  
		case "()\u2082":if(operation==false)
		            {
					 op1=Double.parseDouble(jt.getText());
					 jt.setText(Integer.toBinaryString((int)op1));
					}
		          break;
		case "()\u2088":if(operation==false)
		            {
					 op1=Double.parseDouble(jt.getText());
					 jt.setText(Integer.toOctalString((int)op1));
					}
		          break;
		case "()\u2081\u2086":if(operation==false)
		            {
					 op1=Double.parseDouble(jt.getText());
					 jt.setText(Integer.toHexString((int)op1));
					}
		          break;
        case "~":if(operation==false)
		            {
					 op1=Double.parseDouble(jt.getText());
					 jt.setText(Integer.toString(~(int)op1));
					}
		          break;				  
		}
	 }
  }
 
 

Execution

C:/jgyan/Desktop> javac calculator.java
C:/jgyan/Desktop> java calculator