&

Tuesday, May 14, 2013

Membuat Kalkulator sederhana dengan menggunakan Java NetBeans

 Membuat Kalkulator sederhana dengan menggunakan Java NetBeans


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package kalkulator;

/**
 *
 * @author User
 */
public class kalkulator_sederhana extends javax.swing.JFrame {
String angka;
Double total, angka1, angka2;
int pilih;

    /**
     * Creates new form kalkulator_sederhana
     */
    public kalkulator_sederhana() {
        initComponents();
        angka="";
    }

    /**
     * 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">                         
    private void initComponents() {

        cmd7 = new javax.swing.JButton();
        cmdtampil = new javax.swing.JTextField();
        cmd8 = new javax.swing.JButton();
        cmd9 = new javax.swing.JButton();
        cmdplusmin = new javax.swing.JButton();
        cmdclear = new javax.swing.JButton();
        cmd4 = new javax.swing.JButton();
        cmd5 = new javax.swing.JButton();
        cmd6 = new javax.swing.JButton();
        cmd1 = new javax.swing.JButton();
        cmd2 = new javax.swing.JButton();
        cmd3 = new javax.swing.JButton();
        cmdpersen = new javax.swing.JButton();
        cmd0 = new javax.swing.JButton();
        cmdkoma = new javax.swing.JButton();
        cmdkurang = new javax.swing.JButton();
        cmdtambah = new javax.swing.JButton();
        cmdbagi = new javax.swing.JButton();
        cmdkali = new javax.swing.JButton();
        cmdsamadengan = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

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

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

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

        cmdplusmin.setText("+/-");

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

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

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

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

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

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

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

        cmdpersen.setText("%");
        cmdpersen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdpersenActionPerformed(evt);
            }
        });

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

        cmdkoma.setText(",");

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

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

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

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

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

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(cmdtampil)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(cmd7)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(cmd8)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(cmd9, 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)
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(cmd4)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(cmd5))
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(cmd1)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(cmd2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                            .addGap(12, 12, 12)
                                            .addComponent(cmd6))
                                        .addGroup(layout.createSequentialGroup()
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                            .addComponent(cmd3)))))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(cmdpersen)
                                .addGap(2, 2, 2)
                                .addComponent(cmd0, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(cmdkoma, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(40, 40, 40)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(cmdplusmin)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cmdclear, 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(cmdtambah, javax.swing.GroupLayout.DEFAULT_SIZE, 49, Short.MAX_VALUE)
                                    .addComponent(cmdkurang, 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)
                                    .addComponent(cmdbagi)
                                    .addComponent(cmdkali)))
                            .addComponent(cmdsamadengan, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cmdtampil, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmd7)
                    .addComponent(cmd8)
                    .addComponent(cmd9)
                    .addComponent(cmdplusmin)
                    .addComponent(cmdclear))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmd4)
                    .addComponent(cmd5)
                    .addComponent(cmd6)
                    .addComponent(cmdkurang)
                    .addComponent(cmdkali))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmd1)
                    .addComponent(cmd2)
                    .addComponent(cmd3)
                    .addComponent(cmdtambah)
                    .addComponent(cmdbagi))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmdpersen)
                    .addComponent(cmd0)
                    .addComponent(cmdkoma)
                    .addComponent(cmdsamadengan))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                       

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

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

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

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

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

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

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

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

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

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

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

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

        }
    }                                            

    private void cmdclearActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        angka1=0.0;
        angka2=0.0;
        total=0.0;
        angka="";
        cmdtampil.setText("");
    }                                       

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

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

    }                                      

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

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

        }
    }                                        

    /**
     * @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(kalkulator_sederhana.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(kalkulator_sederhana.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(kalkulator_sederhana.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(kalkulator_sederhana.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 kalkulator_sederhana().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                    
    private javax.swing.JButton cmd0;
    private javax.swing.JButton cmd1;
    private javax.swing.JButton cmd2;
    private javax.swing.JButton cmd3;
    private javax.swing.JButton cmd4;
    private javax.swing.JButton cmd5;
    private javax.swing.JButton cmd6;
    private javax.swing.JButton cmd7;
    private javax.swing.JButton cmd8;
    private javax.swing.JButton cmd9;
    private javax.swing.JButton cmdbagi;
    private javax.swing.JButton cmdclear;
    private javax.swing.JButton cmdkali;
    private javax.swing.JButton cmdkoma;
    private javax.swing.JButton cmdkurang;
    private javax.swing.JButton cmdpersen;
    private javax.swing.JButton cmdplusmin;
    private javax.swing.JButton cmdsamadengan;
    private javax.swing.JButton cmdtambah;
    private javax.swing.JTextField cmdtampil;
    // End of variables declaration                  
}




 Membuat Kalkulator sederhana dengan menggunakan Java NetBeans

 

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Best CD Rates