Breaking News

GUI Calculator project in Java

GUI Calculator project in Java







Calculator in Java 



Complete Java code

Copy and paste this code into NetBeans and eclipse and run ...



package calculator;


public class Calculator extends javax.swing.JFrame {

 
    public Calculator() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        bdelete = new javax.swing.JButton();
        bc = new javax.swing.JButton();
        bce = new javax.swing.JButton();
        bsign = new javax.swing.JButton();
        b7 = new javax.swing.JButton();
        b8 = new javax.swing.JButton();
        b9 = new javax.swing.JButton();
        bdiv = new javax.swing.JButton();
        b4 = new javax.swing.JButton();
        b5 = new javax.swing.JButton();
        b6 = new javax.swing.JButton();
        bmulti = new javax.swing.JButton();
        b1 = new javax.swing.JButton();
        b2 = new javax.swing.JButton();
        b3 = new javax.swing.JButton();
        bminus = new javax.swing.JButton();
        b0 = new javax.swing.JButton();
        bdot = new javax.swing.JButton();
        bplus = new javax.swing.JButton();
        bequal = new javax.swing.JButton();
        display = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        bdelete.setText("DEL");
        bdelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bdeleteActionPerformed(evt);
            }
        });

        bc.setText("CE");
        bc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bcActionPerformed(evt);
            }
        });

        bce.setText("C");
        bce.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bceActionPerformed(evt);
            }
        });

        bsign.setText("+/-");
        bsign.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bsignActionPerformed(evt);
            }
        });

        b7.setText("7");
        b7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b7ActionPerformed(evt);
            }
        });

        b8.setText("8");
        b8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b8ActionPerformed(evt);
            }
        });

        b9.setText("9");
        b9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b9ActionPerformed(evt);
            }
        });

        bdiv.setText("/");
        bdiv.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bdivActionPerformed(evt);
            }
        });

        b4.setText("4");
        b4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b4ActionPerformed(evt);
            }
        });

        b5.setText("5");
        b5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b5ActionPerformed(evt);
            }
        });

        b6.setText("6");
        b6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b6ActionPerformed(evt);
            }
        });

        bmulti.setText("*");
        bmulti.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bmultiActionPerformed(evt);
            }
        });

        b1.setText("1");
        b1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b1ActionPerformed(evt);
            }
        });

        b2.setText("2");
        b2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b2ActionPerformed(evt);
            }
        });

        b3.setText("3");
        b3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b3ActionPerformed(evt);
            }
        });

        bminus.setText("-");
        bminus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bminusActionPerformed(evt);
            }
        });

        b0.setText("0");
        b0.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b0ActionPerformed(evt);
            }
        });

        bdot.setText(".");
        bdot.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bdotActionPerformed(evt);
            }
        });

        bplus.setText("+");
        bplus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bplusActionPerformed(evt);
            }
        });

        bequal.setText("=");
        bequal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bequalActionPerformed(evt);
            }
        });

        display.setBackground(new java.awt.Color(255, 204, 255));
        display.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        display.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        display.setText("0");
        display.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 3));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(59, 59, 59)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(display, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(b0, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(b1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(b4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(b7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(bdelete, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(bc, javax.swing.GroupLayout.DEFAULT_SIZE, 48, Short.MAX_VALUE)
                            .addComponent(b8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(b5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(b2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(bdot, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(bce, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(b9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(b6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(b3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(bplus, javax.swing.GroupLayout.DEFAULT_SIZE, 49, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(bsign, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(bdiv, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(bmulti, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(bminus, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(bequal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap(122, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addComponent(display, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bdelete)
                    .addComponent(bc)
                    .addComponent(bce)
                    .addComponent(bsign))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(b7)
                    .addComponent(b8)
                    .addComponent(b9)
                    .addComponent(bdiv))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(b4)
                    .addComponent(b5)
                    .addComponent(b6)
                    .addComponent(bmulti))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(b1)
                    .addComponent(b2)
                    .addComponent(b3)
                    .addComponent(bminus))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(b0)
                    .addComponent(bdot)
                    .addComponent(bplus)
                    .addComponent(bequal))
                .addContainerGap(29, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void b0ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b0ActionPerformed
        if(display.getText().equals("ERROR"))
            ; //nothing to do
        else
            if(display.getText().equals("0")||isOperatorPressed)
                display.setText("0");
            else
                display.setText(display.getText()+"0");
        isOperatorPressed=false;
    }//GEN-LAST:event_b0ActionPerformed

    private void b1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b1ActionPerformed
       if(display.getText().equals("ERROR"))
        ; //nothing will happen     
    else if(display.getText().equals("0")||isOperatorPressed)
   display.setText("1");
  else 
   display.setText(display.getText()+1);
  isOperatorPressed=false;
    }//GEN-LAST:event_b1ActionPerformed

    private void b2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b2ActionPerformed
         if(display.getText().equals("ERROR"))
        ; //nothing will happen     
    else if(display.getText().equals("0")||isOperatorPressed)
   display.setText("2");
  else 
   display.setText(display.getText()+2);
  isOperatorPressed=false;
    }//GEN-LAST:event_b2ActionPerformed

    private void b3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b3ActionPerformed
     if(display.getText().equals("ERROR"))
        ; //nothing will happen     
    else if(display.getText().equals("0")||isOperatorPressed)
   display.setText("3");
  else 
   display.setText(display.getText()+3);
  isOperatorPressed=false;
    }//GEN-LAST:event_b3ActionPerformed

    private void b4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b4ActionPerformed
        if(display.getText().equals("ERROR"))
        ; //nothing will happen     
    else if(display.getText().equals("0")||isOperatorPressed)
   display.setText("4");
  else 
   display.setText(display.getText()+4);
  isOperatorPressed=false;
    }//GEN-LAST:event_b4ActionPerformed

    private void b5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b5ActionPerformed
        if(display.getText().equals("ERROR"))
        ; //nothing will happen     
    else if(display.getText().equals("0")||isOperatorPressed)
   display.setText("5");
  else 
   display.setText(display.getText()+5);
  isOperatorPressed=false;
    }//GEN-LAST:event_b5ActionPerformed

    private void b6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b6ActionPerformed
       if(display.getText().equals("ERROR"))
        ; //nothing will happen     
    else if(display.getText().equals("0")||isOperatorPressed)
   display.setText("6");
  else 
   display.setText(display.getText()+6);
  isOperatorPressed=false;
    }//GEN-LAST:event_b6ActionPerformed

    private void b7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b7ActionPerformed
      if(display.getText().equals("ERROR"))
        ; //nothing will happen     
    else if(display.getText().equals("0")||isOperatorPressed)
   display.setText("7");
  else 
   display.setText(display.getText()+7);
  isOperatorPressed=false;
    }//GEN-LAST:event_b7ActionPerformed

    private void b8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b8ActionPerformed
        if(display.getText().equals("ERROR"))
        ; //nothing will happen     
    else if(display.getText().equals("0")||isOperatorPressed)
   display.setText("8");
  else 
   display.setText(display.getText()+8);
  isOperatorPressed=false;
    }//GEN-LAST:event_b8ActionPerformed

    private void b9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b9ActionPerformed
     if(display.getText().equals("ERROR"))
        ; //nothing will happen     
    else if(display.getText().equals("0")||isOperatorPressed)
   display.setText("9");
  else 
   display.setText(display.getText()+9);
  isOperatorPressed=false;
    }//GEN-LAST:event_b9ActionPerformed

    private void bdeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bdeleteActionPerformed
        if(display.getText().equals("ERROR")||display.getText().equals("0")||isOperatorPressed)
   ;//nothing will happen
        else{
            if(display.getText().length()>1)
                 display.setText(display.getText().substring(0,display.getText().length()-1));
            else
                display.setText("0");
        if(display.getText().indexOf('.')==-1)
  isDot=false;
        }
    }//GEN-LAST:event_bdeleteActionPerformed

    private void bcActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bcActionPerformed
       if(display.getText().equals("ERROR"))
        ; //nothing will happen     
 else{ 
  display.setText("0");
  isDot=false;
 }
    }//GEN-LAST:event_bcActionPerformed

    private void bceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bceActionPerformed
        display.setText("0");
 isOperatorPressed=false;
 whichOperand=false;
 operator=0;
 isDot=false;
    }//GEN-LAST:event_bceActionPerformed

    private void bsignActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bsignActionPerformed
        if(display.getText().equals("ERROR"))
        ; //nothing will happen     
 else{ 
   if(display.getText().equals("0"))
  ;//nothing will happen
   else{
       if(Double.parseDouble(display.getText()) > 0.0)
   display.setText("-"+display.getText());
       else
   display.setText(display.getText().substring(1,display.getText().length()));
     }
   }
    }//GEN-LAST:event_bsignActionPerformed

    private void bdotActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bdotActionPerformed
        if(display.getText().equals("ERROR"))
       ; //nothing will happen     
 else if(display.getText().equals("0")||isOperatorPressed){
   display.setText("0.");
   isDot=true;
  }
  else{ 
   if(isDot)
     ;//nothing will happen
   else
     display.setText(display.getText()+".");
   isDot=true;
  }
  isOperatorPressed=false;
     
    }//GEN-LAST:event_bdotActionPerformed

    private void bplusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bplusActionPerformed
       if(display.getText().equals("ERROR"))
        ; //nothing will happen     
        else{ 
     if(whichOperand==false){
    data1=Double.parseDouble(display.getText());
    whichOperand=true;
  }
  else{
    if(isOperatorPressed)
      ;//nothing will happen
    else{
     data2=Double.parseDouble(display.getText());
     
     switch(operator)
     {
                        case 1:
                               data1=data1+data2;
      break;
   case 2:
         data1=data1-data2;
      break;
   case 3:
      data1=data1*data2;
      break;
   case 4:
      if(data2!=0.0)
         data1=data1/data2;
      else
      display.setText("ERROR");
      break;
     }
     if(display.getText().equals("ERROR")){
        isOperatorPressed=false;
        operator=0;
     }
     else if(data1==Math.floor(data1))
       display.setText(""+(int)Math.floor(data1));
     else
       display.setText(""+data1);
    }
    
  }
  
    if(!display.getText().equals("ERROR")){
   isOperatorPressed=true;
   operator=1;
  }
  } 
    }//GEN-LAST:event_bplusActionPerformed

    private void bminusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bminusActionPerformed
        if(display.getText().equals("ERROR"))
        ; //nothing will happen     
    else {
  if(whichOperand==false){
    data1=Double.parseDouble(display.getText());
    whichOperand=true;
  }
  else{
    if(isOperatorPressed)
      ;//nothing will happen
    else{
     data2=Double.parseDouble(display.getText());
     switch(operator)
     {
      case 1:
            data1=data1+data2;
      break;
   case 2:
         data1=data1-data2;
      break;
   case 3:
      data1=data1*data2;
      break;
   case 4:
       if(data2!=0.0)
         data1=data1/data2;
      else
      display.setText("ERROR");
      break;
     }
     if(display.getText().equals("ERROR")){
        isOperatorPressed=false;
        operator=0;
     }
     else if(data1==Math.floor(data1))
       display.setText(""+(int)Math.floor(data1));
     else
       display.setText(""+data1);
    }
    
  }
  if(!display.getText().equals("ERROR")){
   isOperatorPressed=true;
   operator=2;
  }
    
  }   
    }//GEN-LAST:event_bminusActionPerformed

    private void bmultiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bmultiActionPerformed
       if(display.getText().equals("ERROR"))
        ; //nothing will happen     
    else {
  if(whichOperand==false){
    data1=Double.parseDouble(display.getText());
    whichOperand=true;
  }
  else{
    if(isOperatorPressed)
      ;//nothing will happen
    else{
     data2=Double.parseDouble(display.getText());
     switch(operator)
     {
      case 1:
            data1=data1+data2;
      break;
   case 2:
         data1=data1-data2;
      break;
   case 3:
      data1=data1*data2;
      break;
   case 4:
       if(data2!=0.0)
         data1=data1/data2;
      else
      display.setText("ERROR");
      break;
     }
     if(display.getText().equals("ERROR")){
        isOperatorPressed=false;
        operator=0;
     }
     else if(data1==Math.floor(data1))
       display.setText(""+(int)Math.floor(data1));
     else
       display.setText(""+data1);
    }
    
  }
  if(!display.getText().equals("ERROR")){
   isOperatorPressed=true;
   operator=3;
  }
    
  }    
    }//GEN-LAST:event_bmultiActionPerformed

    private void bdivActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bdivActionPerformed
       if(display.getText().equals("ERROR"))
        ; //nothing will happen     
    else {
  if(whichOperand==false){
    data1=Double.parseDouble(display.getText());
    whichOperand=true;
  }
  else{
    if(isOperatorPressed)
      ;//nothing will happen
    else{
     data2=Double.parseDouble(display.getText());
     switch(operator)
     {
      case 1:
            data1=data1+data2;
      break;
   case 2:
         data1=data1-data2;
      break;
   case 3:
      data1=data1*data2;
      break;
   case 4:
      if(data2!=0.0)
         data1=data1/data2;
      else
      display.setText("ERROR");
      break;
     }
     if(display.getText().equals("ERROR")){
        isOperatorPressed=false;
        operator=0;
     }
     else if(data1==Math.floor(data1))
       display.setText(""+(int)Math.floor(data1));
     else
       display.setText(""+data1);
    }
    }
    
  
  if(!display.getText().equals("ERROR")){
   isOperatorPressed=true;
   operator=4;
  }
  }    
    }//GEN-LAST:event_bdivActionPerformed

    private void bequalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bequalActionPerformed
        if(display.getText().equals("ERROR"))
        ; //nothing will happen     
    else {
  if(whichOperand==false){
    data1=Double.parseDouble(display.getText());
    whichOperand=false; //remain false only
    isOperatorPressed=true;
  }
  else{
    if(isOperatorPressed){
      data2=data1;
      switch(operator)
      {
      case 1:
            data1=data1+data2;
      break;
   case 2:
         data1=data1-data2;
      break;
   case 3:
      data1=data1*data2;
      break;
   case 4:
      if(data2!=0.0)
         data1=data1/data2;
      else
      display.setText("ERROR");
      break;
      }
   if(display.getText().equals("ERROR")){
        isOperatorPressed=false;
        operator=0;
      }
     else if(data1==Math.floor(data1))
       display.setText(""+(int)Math.floor(data1));
     else
       display.setText(""+data1);
    }
    else{
     data2=Double.parseDouble(display.getText());
     switch(operator)
     {
      case 1:
            data1=data1+data2;
      break;
   case 2:
         data1=data1-data2;
      break;
   case 3:
      data1=data1*data2;
      break;
   case 4:
      if(data2!=0.0)
         data1=data1/data2;
      else
      display.setText("ERROR");
      break;
     }
     if(display.getText().equals("ERROR")){
        isOperatorPressed=false;
        operator=0;
     }
     else if(data1==Math.floor(data1))
       display.setText(""+(int)Math.floor(data1));
     else
       display.setText(""+data1);
    }
    whichOperand=false;
    }
    
  
  if(!display.getText().equals("ERROR")){
   isOperatorPressed=true;
   operator=5;
  }
  }    
    }//GEN-LAST:event_bequalActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Calculator().setVisible(true);
            }
        });
    }

    
    private double data1,data2;
    private boolean isDot=false; //if dot is pressed then true
    private boolean isOperatorPressed=false; //ture when operator is pressed
    private boolean whichOperand=false; //true for second operand
    private int operator=0; //0-no selection, 1-plus,2-minus, 3-multi, 4-div, 5-equal
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton b0;
    private javax.swing.JButton b1;
    private javax.swing.JButton b2;
    private javax.swing.JButton b3;
    private javax.swing.JButton b4;
    private javax.swing.JButton b5;
    private javax.swing.JButton b6;
    private javax.swing.JButton b7;
    private javax.swing.JButton b8;
    private javax.swing.JButton b9;
    private javax.swing.JButton bc;
    private javax.swing.JButton bce;
    private javax.swing.JButton bdelete;
    private javax.swing.JButton bdiv;
    private javax.swing.JButton bdot;
    private javax.swing.JButton bequal;
    private javax.swing.JButton bminus;
    private javax.swing.JButton bmulti;
    private javax.swing.JButton bplus;
    private javax.swing.JButton bsign;
    private javax.swing.JLabel display;
    // End of variables declaration//GEN-END:variables
}



Output






For big minor and major project contact us on

vijay.ajay70333@gmail.com







Thanks . . .


No comments