Bitcoin

Bitcoin
Bitcoin

Java Calculators Code

A CODE FOR CALCULATOR
/* Java Program Example - Make Calculator */
  
import java.util.Scanner;

public class JavaProgram
{
    public static void main(String args[])
    {
        float a, b, res;
        char choice, ch;
        Scanner scan = new Scanner(System.in);
  
        do
        {
            System.out.print("1. Addition\n");
            System.out.print("2. Subtraction\n");
            System.out.print("3. Multiplication\n");
            System.out.print("4. Division\n");
            System.out.print("5. Exit\n\n");
            System.out.print("Enter Your Choice : ");
            choice = scan.next().charAt(0);
            switch(choice)
            {
                case '1' : System.out.print("Enter Two Number : ");
                    a = scan.nextFloat();
                    b = scan.nextFloat();
                    res = a + b;
                    System.out.print("Result = " + res);
                    break;
                case '2' : System.out.print("Enter Two Number : ");
                    a = scan.nextFloat();
                    b = scan.nextFloat();
                    res = a - b;
                    System.out.print("Result = " + res);
                    break;
                case '3' : System.out.print("Enter Two Number : ");
                    a = scan.nextFloat();
                    b = scan.nextFloat();
                    res = a * b;
                    System.out.print("Result = " + res);
                    break;
                case '4' : System.out.print("Enter Two Number : ");
                    a = scan.nextFloat();
                    b = scan.nextFloat();
                    res = a / b;
                    System.out.print("Result = " + res);
                    break;
                case '5' : System.exit(0);
                    break;
                default : System.out.print("Wrong Choice!!!");
                    break;
            }
            System.out.print("\n---------------------------------------\n");
        }while(choice != 5);       
    }
}
GREAT CODE FOR CALCULATOR
//importing all essential packages(Step 1)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
 
//Creating an another seperate class CalculatorFrame(Step 4)
class CalculatorFrame extends JFrame implements ActionListener 
{
 /* Creating all components  and a container
        that is going to be used in our program (Step 6)*/
 Container c;
 JLabel lbl;
 JTextField tf;
 JRadioButton on;
 JRadioButton off;
 JButton zero;
 JButton one;
 JButton two;
 JButton three;
 JButton four;
 JButton five;
 JButton six;
 JButton seven;
 JButton eight;
 JButton nine;
 JButton dot;
 JButton clear;
 JButton backspace;
 JButton equal;
 JButton mul;
 JButton div;
 JButton plus;
 JButton minus;
 JButton sqroot;
 
 //Creating two double and one integer type variables(Step 7)
 double num,ans;
 int calculation;
 
 // Creating constructor of CalculatorFrame class (Step 8)
 CalculatorFrame()
 {
  //getting the content pane using getContentPane() method (Step 9)
  c=this.getContentPane();
  //Setting layout of the container to null (Step 10);
  c.setLayout(null);
  // Setting the background color of container (Step 11)
  c.setBackground(Color.PINK);
  // creating objects of all components (Step 12)
  tf=new JTextField();
  lbl=new JLabel();
  on=new JRadioButton("on");
  off=new JRadioButton("off");
  seven=new JButton("7");
  eight=new JButton("8");
  nine=new JButton("9");
  four=new JButton("4");
  five=new JButton("5");
  six=new JButton("6");
  one=new JButton("1");
  two=new JButton("2");
  three=new JButton("3");
  dot=new JButton(".");
  zero=new JButton("0");
  equal=new JButton("=");
  plus=new JButton("+");
  minus=new JButton("-");
  mul=new JButton("x");
  div=new JButton("/");
  clear=new JButton("C");
  backspace=new JButton("<--");
  sqroot=new JButton("sqrt");
  
  
        //Adjusting alignment to right for text entered in textfield(Step 13)
        tf.setHorizontalAlignment(SwingConstants.RIGHT);
  
  //Creating object of button group(Step 14)
  ButtonGroup grp=new ButtonGroup();
  grp.add(on);
  grp.add(off);
  on.setSelected(true);
  on.setEnabled(false);
  off.setEnabled(true);
  
  //Setting font size for components(Step 15)
  Font fo=new Font("Arial",Font.BOLD,20);
  tf.setFont(fo);
  seven.setFont(fo);
  eight.setFont(fo);
  nine.setFont(fo);
  four.setFont(fo);
     five.setFont(fo);
  six.setFont(fo);
  one.setFont(fo);
  two.setFont(fo);
  three.setFont(fo);
  dot.setFont(fo);
  zero.setFont(fo);
  equal.setFont(fo);
  plus.setFont(fo);
  minus.setFont(fo);
  mul.setFont(fo);
  div.setFont(fo);
  clear.setFont(fo);
  
  
  
  
  Font font=new Font("Arial",Font.BOLD,15);
  backspace.setFont(font);
 
  /*Setting size and location of components 
    using setBounds() method(Step 16)*/
    
  tf.setBounds(10,40,270,40);
  lbl.setBounds(250,0,50,50);
  on.setBounds(10,95,60,40);
  off.setBounds(10,120,60,40);
  clear.setBounds(80,110,60,40);
  seven.setBounds(10,170,60,40);
  eight.setBounds(80,170,60,40);
  nine.setBounds(150,170,60,40);
  four.setBounds(10,230,60,40);
  five.setBounds(80,230,60,40);
  six.setBounds(150,230,60,40);
  one.setBounds(10,290,60,40);
  two.setBounds(80,290,60,40);
  three.setBounds(150,290,60,40);
  dot.setBounds(10,350,60,40);
  zero.setBounds(80,350,60,40);
  equal.setBounds(150,350,60,40);
  plus.setBounds(220,110,60,40);
  minus.setBounds(220,170,60,40);
  mul.setBounds(220,230,60,40);
  div.setBounds(220,290,60,40);
  backspace.setBounds(150,110,60,40);
  sqroot.setBounds(220,350,60,40);
  
  
  /*Adding components to container using 
     add() method(Step 17)*/
  c.add(tf);
  c.add(lbl);
  c.add(on);
  c.add(off);
  c.add(seven);
  c.add(eight);
  c.add(nine);
  c.add(four);
  c.add(five);
  c.add(six);
  c.add(one);
  c.add(two);
  c.add(three);
  c.add(dot);
  c.add(zero);
  c.add(equal);
  c.add(plus);
  c.add(minus);
  c.add(mul);
  c.add(div);
  c.add(clear);
  c.add(backspace);
  c.add(sqroot);
  
  //Registering action listener to buttons(Step 18)
  on.addActionListener(this);
  off.addActionListener(this);
  seven.addActionListener(this);
  eight.addActionListener(this);
  nine.addActionListener(this);
  four.addActionListener(this);
  five.addActionListener(this);
  six.addActionListener(this);
  one.addActionListener(this);
  two.addActionListener(this);
  three.addActionListener(this);
  dot.addActionListener(this);
  zero.addActionListener(this);
  equal.addActionListener(this);
  plus.addActionListener(this);
  minus.addActionListener(this);
  mul.addActionListener(this);
  div.addActionListener(this);
  clear.addActionListener(this);
  backspace.addActionListener(this);
  sqroot.addActionListener(this);
  
    
  
 }
 
 
 
 // Performing actionPerformed() method for each registered buttons(Step 19)
 public void actionPerformed(ActionEvent e)
 {
  
    if(e.getSource()==on)
  {
   enable();
  }
  
  else if(e.getSource()==off)
  {
   disable();
  }
  else if(e.getSource()==clear)
  {
   tf.setText(" ");
   lbl.setText(" ");
  }
  
  else if(e.getSource()==backspace)
  {
   int length=tf.getText().length();
    int number=length-1;
    
    
    if(length>0)
    {
     StringBuilder back=new StringBuilder(tf.getText());
     back.deleteCharAt(number);
     tf.setText(back.toString());
     
    }
   
  }
  else if(e.getSource()==zero)
  {
   tf.setText(tf.getText()+"0");
  }
  
  
  else if(e.getSource()==one)
  {
    tf.setText(tf.getText()+"1");
  }
  
  else if(e.getSource()==two)
  {
    tf.setText(tf.getText()+"2");
  }
  
  else if(e.getSource()==three)
  {
    tf.setText(tf.getText()+"3");
  }
  
  else if(e.getSource()==four)
  {
        tf.setText(tf.getText()+"4");
  }
  
  else if(e.getSource()==five)
  {
    tf.setText(tf.getText()+"5");
  }
  
  else if(e.getSource()==six)
  {
    tf.setText(tf.getText()+"6");
  }
  
  else if(e.getSource()==seven)
  {
    tf.setText(tf.getText()+"7");
  }
  
  else if(e.getSource()==eight)
  {
    tf.setText(tf.getText()+"8");
  }
  else if(e.getSource()==nine)
  {
    tf.setText(tf.getText()+"9");
    
  }
  
  else if(e.getSource()==dot)
  {
    tf.setText(tf.getText()+".");
  }
  else if(e.getSource()==plus)
  {
   num=Double.parseDouble(tf.getText());
    tf.setText(" ");
    lbl.setText(num+"+");
    calculation=1;
  }
  else if(e.getSource()==minus)
  {
   num=Double.parseDouble(tf.getText());
    tf.setText(" ");
    lbl.setText(num+"-");
    calculation=2;
  }
  else if(e.getSource()==mul)
  {
   num=Double.parseDouble(tf.getText());
    tf.setText(" ");
    lbl.setText(num+"x");
    calculation=3;
  }
  
  else if(e.getSource()==div)
  {
   num=Double.parseDouble(tf.getText());
    tf.setText(" ");
    lbl.setText(num+"/");
    calculation=4;
  }
  else if(e.getSource()==sqroot)
  {
   num=Double.parseDouble(tf.getText());
   ans=Math.sqrt(Double.parseDouble(tf.getText()));
         tf.setText(Double.toString(ans));
  }
  else if(e.getSource()==equal)
  {
   switch(calculation)
    {
     case 1:
             ans=num + Double.parseDouble(tf.getText());
       tf.setText(Double.toString(ans));
       lbl.setText(" ");
       break;
     case 2:
       ans=num - Double.parseDouble(tf.getText());
       tf.setText(Double.toString(ans));
       lbl.setText(" ");
       break;
     case 3:
       ans=num * Double.parseDouble(tf.getText());
       tf.setText(Double.toString(ans));
       lbl.setText(" ");
       break;
     case 4:
       ans=num / Double.parseDouble(tf.getText());
       tf.setText(Double.toString(ans));
       lbl.setText(" ");
       break;
     
    }
  }
  
 }
 
 
 public void disable()
 {
  tf.setText(" ");
  lbl.setText(" ");
  off.setEnabled(false);
  on.setEnabled(true);
  tf.setEnabled(false);
  dot.setEnabled(false);
  zero.setEnabled(false);
  one.setEnabled(false);
  two.setEnabled(false);
  three.setEnabled(false);
  four.setEnabled(false);
  five.setEnabled(false);
  six.setEnabled(false);
  seven.setEnabled(false);
  eight.setEnabled(false);
  nine.setEnabled(false);
  plus.setEnabled(false);
  minus.setEnabled(false);
  mul.setEnabled(false);
  div.setEnabled(false);
  equal.setEnabled(false);
  clear.setEnabled(false);
  backspace.setEnabled(false);
 }
 
 public void enable()
 {
  on.setEnabled(false);
  off.setEnabled(true);
  tf.setEnabled(true);
  dot.setEnabled(true);
  zero.setEnabled(true);
  one.setEnabled(true);
  two.setEnabled(true);
  three.setEnabled(true);
  four.setEnabled(true);
  five.setEnabled(true);
  six.setEnabled(true);
  seven.setEnabled(true);
  eight.setEnabled(true);
  nine.setEnabled(true);
  plus.setEnabled(true);
  minus.setEnabled(true);
  mul.setEnabled(true);
  div.setEnabled(true);
  equal.setEnabled(true);
  clear.setEnabled(true);
  backspace.setEnabled(true);
  
 }
 
 
}
 
// Creating a class Calculator(Step 2)
class Calculator 
{
 //Creating main method (Step 3)
 public static void main(String args[])
 {
  /*Creating object of CalculatorFrame 
     class and setting some of its properties (step 5) */
  CalculatorFrame f=new CalculatorFrame();
  f.setTitle("Calculator");
  f.setVisible(true);
  f.setBounds(100,100,300,440);
  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  f.setResizable(false);
  
 } 
}
ANOTHER CODE FOR CALCULATOR
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Mycalculator
{
 GridLayout gridlayout = new GridLayout(4,4);
 JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,bminus,bplus,bdivide,bmultiply,bequal,brem,bpercent;
 Box box;
 JPanel pane;
 JLabel firstscreen, secondscreen, signscreen, resscreen;
 ArrayList<Object> list;
 JPanel firstText = new JPanel();
 JPanel secondText = new JPanel();
 JPanel signText = new JPanel();
 JPanel resText = new JPanel();
 Box theScreen = Box.createHorizontalBox();
 public Mycalculator(){
  box = Box.createVerticalBox();
  list = new ArrayList<Object>();

  firstscreen = new JLabel("");
  firstText.add(firstscreen);firstText.setBackground(Color.BLUE);
  signscreen = new JLabel("");
  signText.add(signscreen);signText.setBackground(Color.BLUE);
  secondscreen = new JLabel("");secondText.setBackground(Color.BLUE);
  secondText.add(secondscreen);
  
  resscreen = new JLabel("");resText.setBackground(Color.BLUE);
  resText.add(resscreen);
  
  theScreen.add(firstText);
  theScreen.add(signText);
  theScreen.add(secondText);
  theScreen.add(resText);
  box.add(theScreen);
  box.add(Box.createVerticalStrut(40));

  pane = new JPanel(gridlayout);

  b0 = new JButton("0");
  b1 = new JButton("1");
  b2 = new JButton("2");
  b3 = new JButton("3");
  b4 = new JButton("4");
  b5 = new JButton("5");
  b6 = new JButton("6");
  b7 = new JButton("7");
  b8 = new JButton("8");
  b9 = new JButton("9");
  bequal = new JButton("=");
  bdivide = new JButton("/");
  bmultiply = new JButton("*");
  bplus = new JButton("+");
  bminus = new JButton("-");
  brem = new JButton("REM");
  bpercent = new JButton("%");
  pane.add(b0);
  pane.add(b1);
  pane.add(b2);
  pane.add(b3);
  pane.add(b4);
  pane.add(b5);
  pane.add(b6);
  pane.add(b7);
  pane.add(b8);
  pane.add(b9);
  pane.add(bplus);
  pane.add(bminus);
  pane.add(bmultiply);
  pane.add(bdivide);
  pane.add(bpercent);
  pane.add(brem);
  pane.add(bequal);

  b0.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    resscreen.setText("");
    verify(b0);
    
   }
  });
  b1.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    resscreen.setText("");
    verify(b1);
   }
  });
  b2.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    resscreen.setText("");
    verify(b2);
   }
  });
  b3.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    resscreen.setText("");
    verify(b3);
   }
  });
  b4.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    resscreen.setText("");
    verify(b4);
   }
  });
  b5.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    resscreen.setText("");
    verify(b5);
   }
  });
  b6.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    resscreen.setText("");
    verify(b6);
   }
  });
  b7.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    resscreen.setText("");
    verify(b7);
   }
  });
  b8.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    resscreen.setText("");
    verify(b8);
    
   }
  });
  b9.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    resscreen.setText("");
    verify(b9);
   }
  });
  bplus.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    signverify(bplus);
   }
  });
  bmultiply.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    signverify(bmultiply);
   }
  });
  bminus.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    signverify(bminus);
   }
  });
  brem.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    signverify(brem);
   }
  });
  bdivide.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    signverify(bdivide);
   }
  });
  bequal.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    double first = Double.parseDouble(String.valueOf(firstscreen.getText()));
    double second = Double.parseDouble(String.valueOf(secondscreen.getText()));
    String sign = signscreen.getText();
    if(sign == "+"){
     double results = first + second;
     resscreen.setText(""+results);
     firstscreen.setText("");
     secondscreen.setText("");
     signscreen.setText("");
    }
    if(sign == "*"){
     double results = first * second;
     resscreen.setText(""+results);
     firstscreen.setText("");
     secondscreen.setText("");
     signscreen.setText("");
    }
    if(sign == "-"){
     double results = first - second;
     resscreen.setText(""+results);
     firstscreen.setText("");
     secondscreen.setText("");
     signscreen.setText("");
    }
    if(sign == "REM"){
     double results = first % second;
     resscreen.setText(""+results);
     firstscreen.setText("");
     secondscreen.setText("");
     signscreen.setText("");
    }
    if(sign == "/"){
     double results = first % second;
     resscreen.setText(""+results);
     firstscreen.setText("");
     secondscreen.setText("");
     signscreen.setText("");
    }
    if(sign == "%"){
     double results = first % second;
     resscreen.setText(""+results);
     firstscreen.setText("");
     secondscreen.setText("");
     signscreen.setText("");
    }
   }
  });
  box.add(pane);


  JFrame frame = new JFrame("CORRECT");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.add(box);
  frame.setSize(400,300);
  frame.setVisible(true);
 }
 double result;
 String value = null;

 public void verify(JButton b){
  if(resscreen.getText() == "" || resscreen.getText() == null){
   if(signscreen.getText() == "" || signscreen.getText() == null){
     value = firstscreen.getText()+b.getText();
     firstscreen.setText(value);
   }else{
     value = secondscreen.getText()+b.getText();
     secondscreen.setText(value);
   }
  }
 }
 public void signverify(JButton b){
  if(firstscreen.getText() == "" || firstscreen.getText() == null){
     value = "";
     firstscreen.setText(value);
   }else{
     value = b.getText();
     signscreen.setText(value);
   }
  
 }
 public static void main(String[] args){
  new Mycalculator();
 }
}
ANOTHER CODE FOR CALCULATOR
//Imports are listed in full to show what's being used
//could just import javax.swing.* and java.awt.* etc..
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Container;

public class SimpleCalc implements ActionListener{
 
    JFrame guiFrame;
    JPanel buttonPanel;
    JTextField numberCalc;
    int calcOperation = 0;
    int currentCalc;
    
    //Note: Typically the main method will be in a
    //separate class. As this is a simple one class
    //example it's all in the one class.
    public static void main(String[] args) {
     
         //Use the event dispatch thread for Swing components
         EventQueue.invokeLater(new Runnable()
         {
             
            @Override
             public void run()
             {
                 
                 new SimpleCalc();         
             }
         });
              
    }
    
    public SimpleCalc()
    {
        guiFrame = new JFrame();
        
        //make sure the program exits when the frame closes
        guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        guiFrame.setTitle("Simple Calculator");
        guiFrame.setSize(300,300);
      
        //This will center the JFrame in the middle of the screen
        guiFrame.setLocationRelativeTo(null);
        
        numberCalc = new JTextField();
        numberCalc.setHorizontalAlignment(JTextField.RIGHT);
        numberCalc.setEditable(false);
        
        guiFrame.add(numberCalc, BorderLayout.NORTH);
        
        buttonPanel = new JPanel();
               
        //Make a Grid that has three rows and four columns
        buttonPanel.setLayout(new GridLayout(4,3));   
        guiFrame.add(buttonPanel, BorderLayout.CENTER);
        
        //Add the number buttons
        for (int i=1;i<10;i++)
        {
            addButton(buttonPanel, String.valueOf(i));
        }

        JButton addButton = new JButton("+");
        addButton.setActionCommand("+");
        
        OperatorAction subAction = new OperatorAction(1);
        addButton.addActionListener(subAction);
        
        JButton subButton = new JButton("-");
        subButton.setActionCommand("-");
        
        OperatorAction addAction = new OperatorAction(2);
        subButton.addActionListener(addAction);
        
        JButton equalsButton = new JButton("=");
        equalsButton.setActionCommand("=");
        equalsButton.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent event)
            {
                if (!numberCalc.getText().isEmpty())
                {
                    int number = Integer.parseInt(numberCalc.getText()); 
                    if (calcOperation == 1)
                    {
                        int calculate = currentCalc  + number;
                        numberCalc.setText(Integer.toString(calculate));
                    }
                    else if (calcOperation == 2)
                    {
                        int calculate = currentCalc  - number;
                        numberCalc.setText(Integer.toString(calculate));
                    }
                }
            }
        });
        
        buttonPanel.add(addButton);
        buttonPanel.add(subButton);
        buttonPanel.add(equalsButton);
        guiFrame.setVisible(true);  
    }
    
    //All the buttons are following the same pattern
    //so create them all in one place.
    private void addButton(Container parent, String name)
    {
        JButton but = new JButton(name);
        but.setActionCommand(name);
        but.addActionListener(this);
        parent.add(but);
    }
    
    //As all the buttons are doing the same thing it's
    //easier to make the class implement the ActionListener
    //interface and control the button clicks from one place
    @Override
    public void actionPerformed(ActionEvent event)
    {
        //get the Action Command text from the button
        String action = event.getActionCommand();
        
        //set the text using the Action Command text
        numberCalc.setText(action);       
    }
    
    private class OperatorAction implements ActionListener
    {
        private int operator;
        
        public OperatorAction(int operation)
        {
            operator = operation;
        }
        
        public void actionPerformed(ActionEvent event)
        {
            currentCalc = Integer.parseInt(numberCalc.getText()); 
            calcOperation = operator;
        }
    }
}

CODE FOR CALCULATOR
import javax.swing.*;
import java.awt.event.*;
 
 
class Calc implements ActionListener
{
    JFrame f;
    JTextField t;
    JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,bdiv,bmul,bsub,badd,bdec,beq,bdel,bclr;
 
    static double a=0,b=0,result=0;
    static int operator=0;
 
    Calc()
    {
        f=new JFrame("Calculator");
        t=new JTextField();
        b1=new JButton("1");
        b2=new JButton("2");
        b3=new JButton("3");
        b4=new JButton("4");
        b5=new JButton("5");
        b6=new JButton("6");
        b7=new JButton("7");
        b8=new JButton("8");
        b9=new JButton("9");
        b0=new JButton("0");
        bdiv=new JButton("/");
        bmul=new JButton("*");
        bsub=new JButton("-");
        badd=new JButton("+");
        bdec=new JButton(".");
        beq=new JButton("=");
        bdel=new JButton("Delete");
        bclr=new JButton("Clear");
        
        t.setBounds(30,40,280,30);
        b7.setBounds(40,100,50,40);
        b8.setBounds(110,100,50,40);
        b9.setBounds(180,100,50,40);
        bdiv.setBounds(250,100,50,40);
        
        b4.setBounds(40,170,50,40);
        b5.setBounds(110,170,50,40);
        b6.setBounds(180,170,50,40);
        bmul.setBounds(250,170,50,40);
        
        b1.setBounds(40,240,50,40);
        b2.setBounds(110,240,50,40);
        b3.setBounds(180,240,50,40);
        bsub.setBounds(250,240,50,40);
        
        bdec.setBounds(40,310,50,40);
        b0.setBounds(110,310,50,40);
        beq.setBounds(180,310,50,40);
        badd.setBounds(250,310,50,40);
        
        bdel.setBounds(60,380,100,40);
        bclr.setBounds(180,380,100,40);
        
        f.add(t);
        f.add(b7);
        f.add(b8);
        f.add(b9);
        f.add(bdiv);
        f.add(b4);
        f.add(b5);
        f.add(b6);
        f.add(bmul);
        f.add(b1);
        f.add(b2);
        f.add(b3);
        f.add(bsub);
        f.add(bdec);
        f.add(b0);
        f.add(beq);
        f.add(badd);
        f.add(bdel);
        f.add(bclr);
        
        f.setLayout(null);
        f.setVisible(true);
        f.setSize(350,500);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setResizable(false);
        
        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);
        b4.addActionListener(this);
        b5.addActionListener(this);
        b6.addActionListener(this);
        b7.addActionListener(this);
        b8.addActionListener(this);
        b9.addActionListener(this);
        b0.addActionListener(this);
        badd.addActionListener(this);
        bdiv.addActionListener(this);
        bmul.addActionListener(this);
        bsub.addActionListener(this);
        bdec.addActionListener(this);
        beq.addActionListener(this);
        bdel.addActionListener(this);
        bclr.addActionListener(this);
    }
 
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource()==b1)
            t.setText(t.getText().concat("1"));
        
        if(e.getSource()==b2)
            t.setText(t.getText().concat("2"));
        
        if(e.getSource()==b3)
            t.setText(t.getText().concat("3"));
        
        if(e.getSource()==b4)
            t.setText(t.getText().concat("4"));
        
        if(e.getSource()==b5)
            t.setText(t.getText().concat("5"));
        
        if(e.getSource()==b6)
            t.setText(t.getText().concat("6"));
        
        if(e.getSource()==b7)
            t.setText(t.getText().concat("7"));
        
        if(e.getSource()==b8)
            t.setText(t.getText().concat("8"));
        
        if(e.getSource()==b9)
            t.setText(t.getText().concat("9"));
        
        if(e.getSource()==b0)
            t.setText(t.getText().concat("0"));
        
        if(e.getSource()==bdec)
            t.setText(t.getText().concat("."));
        
        if(e.getSource()==badd)
        {
            a=Double.parseDouble(t.getText());
            operator=1;
            t.setText("");
        }
        
        if(e.getSource()==bsub)
        {
            a=Double.parseDouble(t.getText());
            operator=2;
            t.setText("");
        }
        
        if(e.getSource()==bmul)
        {
            a=Double.parseDouble(t.getText());
            operator=3;
            t.setText("");
        }
        
        if(e.getSource()==bdiv)
        {
            a=Double.parseDouble(t.getText());
            operator=4;
            t.setText("");
        }
        
        if(e.getSource()==beq)
        {
            b=Double.parseDouble(t.getText());
        
            switch(operator)
            {
                case 1: result=a+b;
                    break;
        
                case 2: result=a-b;
                    break;
        
                case 3: result=a*b;
                    break;
        
                case 4: result=a/b;
                    break;
        
                default: result=0;
            }
        
            t.setText(""+result);
        }
        
        if(e.getSource()==bclr)
            t.setText("");
        
        if(e.getSource()==bdel)
        {
            String s=t.getText();
            t.setText("");
            for(int i=0;i<s.length()-1;i++)
            t.setText(t.getText()+s.charAt(i));
        }        
    }
 
    public static void main(String...s)
    {
        new Calc();
    }
}

Facebook