Program Kalkulator Sederhana Bahasa Java Dengan Netbeans

===== Sintag Program ======

package latihan;

/**
 *
 * @author heripurnama
 */
public class kalkulator extends javax.swing.JFrame {
    private int pilihan;

    /**
     * Creates new form kalkulator
     */
   
    public kalkulator() {
        initComponents();
    }
String angka;
       double jumlah,angka1,angka2;
          int pilih;
    /**
     * 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")
    //                          
    private void initComponents() {

        jFrame1 = new javax.swing.JFrame();
        jButton1 = new javax.swing.JButton();
        btnHapus = new javax.swing.JButton();
        btn0 = new javax.swing.JButton();
        btn7 = new javax.swing.JButton();
        btn8 = new javax.swing.JButton();
        btn9 = new javax.swing.JButton();
        btn4 = new javax.swing.JButton();
        btn5 = new javax.swing.JButton();
        btn6 = new javax.swing.JButton();
        btn1 = new javax.swing.JButton();
        btn2 = new javax.swing.JButton();
        btnBagi = new javax.swing.JButton();
        btn3 = new javax.swing.JButton();
        btnKali = new javax.swing.JButton();
        btnKurang = new javax.swing.JButton();
        btnTambah = new javax.swing.JButton();
        btnSamaDengan = new javax.swing.JButton();
        display = new javax.swing.JTextField();
        btnKoma1 = new javax.swing.JButton();
        x2 = new javax.swing.JButton();
        btnPersen = new javax.swing.JButton();
        per = new javax.swing.JButton();
        btnHapus1 = new javax.swing.JButton();

        javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
        jFrame1.getContentPane().setLayout(jFrame1Layout);
        jFrame1Layout.setHorizontalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jFrame1Layout.setVerticalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        jButton1.setText("jButton1");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Kalkulator PiPoLanDo");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        btnHapus.setText("C");
        btnHapus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnHapusActionPerformed(evt);
            }
        });
        getContentPane().add(btnHapus, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 160, 110, 40));

        btn0.setText("0");
        btn0.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn0ActionPerformed(evt);
            }
        });
        getContentPane().add(btn0, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 360, 110, 40));

        btn7.setText("7");
        btn7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn7ActionPerformed(evt);
            }
        });
        getContentPane().add(btn7, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 210, 50, 40));

        btn8.setText("8");
        btn8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn8ActionPerformed(evt);
            }
        });
        getContentPane().add(btn8, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 210, 50, 40));

        btn9.setText("9");
        btn9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn9ActionPerformed(evt);
            }
        });
        getContentPane().add(btn9, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 210, 50, 40));

        btn4.setText("4");
        btn4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn4ActionPerformed(evt);
            }
        });
        getContentPane().add(btn4, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 260, 50, 40));

        btn5.setText("5");
        btn5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn5ActionPerformed(evt);
            }
        });
        getContentPane().add(btn5, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 260, 50, 40));

        btn6.setText("6");
        btn6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn6ActionPerformed(evt);
            }
        });
        getContentPane().add(btn6, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 260, 50, 40));

        btn1.setText("1");
        btn1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn1ActionPerformed(evt);
            }
        });
        getContentPane().add(btn1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 310, 50, 40));

        btn2.setText("2");
        btn2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn2ActionPerformed(evt);
            }
        });
        getContentPane().add(btn2, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 310, 50, 40));

        btnBagi.setText("/");
        btnBagi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBagiActionPerformed(evt);
            }
        });
        getContentPane().add(btnBagi, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 210, 50, 40));

        btn3.setText("3");
        btn3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn3ActionPerformed(evt);
            }
        });
        getContentPane().add(btn3, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 310, 50, 40));

        btnKali.setText("*");
        btnKali.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnKaliActionPerformed(evt);
            }
        });
        getContentPane().add(btnKali, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 260, 50, 40));

        btnKurang.setText("-");
        btnKurang.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnKurangActionPerformed(evt);
            }
        });
        getContentPane().add(btnKurang, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 310, 50, 40));

        btnTambah.setText("+");
        btnTambah.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTambahActionPerformed(evt);
            }
        });
        getContentPane().add(btnTambah, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 360, 110, 40));

        btnSamaDengan.setText("=");
        btnSamaDengan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSamaDenganActionPerformed(evt);
            }
        });
        getContentPane().add(btnSamaDengan, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 160, 110, 40));

        display.setEditable(false);
        display.setFont(new java.awt.Font("Tahoma", 1, 36)); // NOI18N
        display.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        display.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        display.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
        getContentPane().add(display, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 50, 290, 100));

        btnKoma1.setText(",");
        btnKoma1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnKoma1ActionPerformed(evt);
            }
        });
        getContentPane().add(btnKoma1, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 360, 50, 40));

        x2.setText("X2");
        x2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                x2ActionPerformed(evt);
            }
        });
        getContentPane().add(x2, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 210, 50, 40));

        btnPersen.setText("%");
        btnPersen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPersenActionPerformed(evt);
            }
        });
        getContentPane().add(btnPersen, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 263, 50, 40));

        per.setText("1/x");
        per.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                perActionPerformed(evt);
            }
        });
        getContentPane().add(per, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 313, 50, 40));

        btnHapus1.setText("ON");
        btnHapus1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnHapus1ActionPerformed(evt);
            }
        });
        getContentPane().add(btnHapus1, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 160, 50, 40));

        pack();
    }//
                       

    private void btn0ActionPerformed(java.awt.event.ActionEvent evt) {                                    
        // TODO add your handling code here:
        angka += "0";
        display.setText(angka);
    }                                   

    private void btn1ActionPerformed(java.awt.event.ActionEvent evt) {                                    
        // TODO add your handling code here:
        angka += "1";
        display.setText(angka);
    }                                   

    private void btn2ActionPerformed(java.awt.event.ActionEvent evt) {                                    
        // TODO add your handling code here:
        angka += "2";
        display.setText(angka);
    }                                   

    private void btn3ActionPerformed(java.awt.event.ActionEvent evt) {                                    
        // TODO add your handling code here:
        angka += "3";
        display.setText(angka);
    }                                   

    private void btn4ActionPerformed(java.awt.event.ActionEvent evt) {                                    
        // TODO add your handling code here:
        angka += "4";
        display.setText(angka);
    }                                   

    private void btn5ActionPerformed(java.awt.event.ActionEvent evt) {                                    
        // TODO add your handling code here:
        angka += "5";
        display.setText(angka);
    }                                   

    private void btn6ActionPerformed(java.awt.event.ActionEvent evt) {                                    
        // TODO add your handling code here:
        angka += "6";
display.setText(angka);
    }                                   

    private void btn7ActionPerformed(java.awt.event.ActionEvent evt) {                                    
        // TODO add your handling code here:
        angka += "7";
display.setText(angka);
    }                                   

    private void btn8ActionPerformed(java.awt.event.ActionEvent evt) {                                    
        // TODO add your handling code here:
        angka += "8";
display.setText(angka);
    }                                   

    private void btn9ActionPerformed(java.awt.event.ActionEvent evt) {                                    
        // TODO add your handling code here:
        angka += "9";
display.setText(angka);
    }                                   

    private void btnHapusActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
display.setText(" ");
angka1=0;
angka2=0;
jumlah=0;
angka=" ";
    }                                       

    private void btnTambahActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        angka1=Double.parseDouble(angka);
display.setText("+");
angka=" ";
pilih=1;
    }                                        

    private void btnKurangActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        angka1=Double.parseDouble(angka);
display.setText("-");
angka=" ";
pilih=2;
    }                                        

    private void btnKaliActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
        angka1=Double.parseDouble(angka);
display.setText("x");
angka=" ";
pilih=3;
    }                                      

    private void btnBagiActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
        angka1=Double.parseDouble(angka);
display.setText("/");
angka=" ";
pilih=4;
    }                                      

    private void btnSamaDenganActionPerformed(java.awt.event.ActionEvent evt) {                                             
        // TODO add your handling code here:
        switch(pilih){
        case 1:
            angka2 = Double.parseDouble(angka);
            jumlah = angka1 + angka2;
            angka = Double.toString(jumlah);
            display.setText(angka);
            break;
        case 2:
            angka2 = Double.parseDouble(angka);
            jumlah = angka1 - angka2;
            angka = Double.toString(jumlah);
            display.setText(angka);
            break;
        case 3:
            angka2 = Double.parseDouble(angka);
            jumlah = angka1 * angka2;
            angka = Double.toString(jumlah);
            display.setText(angka);
            break;
        case 4:
            angka2 = Double.parseDouble(angka);
            jumlah = angka1 / angka2;
            angka = Double.toString(jumlah);
            display.setText(angka);
            break;
            default:
            break;
         }
    }                                            

    private void btnKoma1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        angka += ".";
        display.setText(angka);
    }                                       

    private void x2ActionPerformed(java.awt.event.ActionEvent evt) {                                  
        // TODO add your handling code here:
         angka1 = Double.parseDouble(angka);
            jumlah = angka1 * angka1;
            angka = Double.toString(jumlah);
            display.setText(angka);
    }                                 

    private void btnPersenActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
         angka1 = Double.parseDouble(angka);
            jumlah = angka1 / 100;
            angka = Double.toString(jumlah);
            display.setText(angka);
    }                                        

    private void perActionPerformed(java.awt.event.ActionEvent evt) {                                   
        // TODO add your handling code here:
        angka1=Double.parseDouble(angka);
           display.setText("1/"+angka);
    }                                  

    private void btnHapus1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        display.setText(" ");
angka=" ";
    }                                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //
        /* 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(kalkulator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(kalkulator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(kalkulator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(kalkulator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //


        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new RunnableImpl());
    }
    // Variables declaration - do not modify                    
    private javax.swing.JButton btn0;
    private javax.swing.JButton btn1;
    private javax.swing.JButton btn2;
    private javax.swing.JButton btn3;
    private javax.swing.JButton btn4;
    private javax.swing.JButton btn5;
    private javax.swing.JButton btn6;
    private javax.swing.JButton btn7;
    private javax.swing.JButton btn8;
    private javax.swing.JButton btn9;
    private javax.swing.JButton btnBagi;
    private javax.swing.JButton btnHapus;
    private javax.swing.JButton btnHapus1;
    private javax.swing.JButton btnKali;
    private javax.swing.JButton btnKoma1;
    private javax.swing.JButton btnKurang;
    private javax.swing.JButton btnPersen;
    private javax.swing.JButton btnSamaDengan;
    private javax.swing.JButton btnTambah;
    private javax.swing.JTextField display;
    private javax.swing.JButton jButton1;
    private javax.swing.JFrame jFrame1;
    private javax.swing.JButton per;
    private javax.swing.JButton x2;
    // End of variables declaration                  

    private static class RunnableImpl implements Runnable {

        public RunnableImpl() {
        }

        public void run() {
            new kalkulator().setVisible(true);
        }
    }
}
=========== output ==============
    Blogger Comment
    Facebook Comment

0 komentar:

Post a comment

Thanks For Your Feedback.