Blogroll

Senin, 17 Desember 2012

program kakulator menggunakan java

Posted by Unknown On 17.47 No comments
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utspbo2;

import java.text.DecimalFormat;
import sun.text.resources.FormatData;

/**
 *
 * @author DEGUNG
 */
public class Kalkulator extends javax.swing.JFrame {
    boolean koma, mulaiBaru, nolLagi;
    double nilai1, nilai2, hasil;
    int proses, hasilInt;
    DecimalFormat formatData = new DecimalFormat("#.#####");
    /**
     * Creates new form Kalkulator
     */
    public Kalkulator() {
        initComponents();
        mulaiBaru=true;
        koma=false;
        nolLagi=true;
        nilai1=0;
        nilai2=0;
        hasil=0;
    }

    /**
     * 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() {

        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();
        jButton9 = new javax.swing.JButton();
        jButton10 = new javax.swing.JButton();
        jButton11 = new javax.swing.JButton();
        jButton12 = new javax.swing.JButton();
        jButton13 = new javax.swing.JButton();
        jButton14 = new javax.swing.JButton();
        jButton15 = new javax.swing.JButton();
        jButton16 = new javax.swing.JButton();
        jButton17 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Kalkulator");

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        jButton17.setText("Clear");
        jButton17.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton17ActionPerformed(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.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton3)
                        .addGap(18, 18, 18)
                        .addComponent(jButton13))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton6)
                        .addGap(18, 18, 18)
                        .addComponent(jButton14))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton7)
                            .addComponent(jButton10))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton8)
                            .addComponent(jButton11))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButton12)
                                .addGap(18, 18, 18)
                                .addComponent(jButton16))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButton9)
                                .addGap(18, 18, 18)
                                .addComponent(jButton15)))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton17))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton1, jButton10, jButton4, jButton7});

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton11, jButton2, jButton3, jButton5, jButton6, jButton8, jButton9});

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton13, jButton14, jButton15, jButton16});

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jTextField1, jTextField2});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(1, 1, 1)
                        .addComponent(jTextField1))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton1)
                            .addComponent(jButton2)
                            .addComponent(jButton3)
                            .addComponent(jButton13))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton4)
                            .addComponent(jButton5)
                            .addComponent(jButton6)
                            .addComponent(jButton14))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton7)
                    .addComponent(jButton8)
                    .addComponent(jButton9)
                    .addComponent(jButton15)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton10)
                    .addComponent(jButton11)
                    .addComponent(jButton12)
                    .addComponent(jButton16)
                    .addComponent(jButton17))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

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

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                       
    if (mulaiBaru){
        jTextField2.setText("1");
        mulaiBaru=false;
    }
    else jTextField2.setText(jTextField2.getText() + "1");
    }                                      

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                       
    if (mulaiBaru){
        jTextField2.setText("2");
        mulaiBaru=false;
    }
    else jTextField2.setText(jTextField2.getText() + "2");
    }                                      

    private void jButton17ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        jTextField1.setText("");
        jTextField2.setText("");
        mulaiBaru=true;
        koma=false;
        nolLagi=true;
        proses=0;
        nilai1=0;
        nilai2=0;
        hasil=0;
    }                                       

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                       
    if (mulaiBaru){
        jTextField2.setText("3");
        mulaiBaru=false;
    }
    else jTextField2.setText(jTextField2.getText() + "3");
    }                                      

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                       
    if (mulaiBaru){
        jTextField2.setText("4");
        mulaiBaru=false;
    }
    else jTextField2.setText(jTextField2.getText() + "4");
    }                                      

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                       
    if (mulaiBaru){
        jTextField2.setText("5");
        mulaiBaru=false;
    }
    else jTextField2.setText(jTextField2.getText() + "5");
    }                                      

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {                                       
    if (mulaiBaru){
        jTextField2.setText("6");
        mulaiBaru=false;
    }
    else jTextField2.setText(jTextField2.getText() + "6");
    }                                      

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {                                       
    if (mulaiBaru){
        jTextField2.setText("7");
        mulaiBaru=false;
    }
    else jTextField2.setText(jTextField2.getText() + "7");
    }                                      

    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {                                       
    if (mulaiBaru){
        jTextField2.setText("8");
        mulaiBaru=false;
    }
    else jTextField2.setText(jTextField2.getText() + "8");
    }                                      

    private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {                                       
    if (mulaiBaru){
        jTextField2.setText("9");
        mulaiBaru=false;
    }
    else jTextField2.setText(jTextField2.getText() + "9");
    }                                      

    private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {                                        
    if (mulaiBaru){
        if (jTextField2.getText().isEmpty() || nolLagi==true){
            jTextField2.setText("0");
            nolLagi=false;
        }
        else{
            mulaiBaru=false;
            jTextField2.setText(jTextField2.getText() + "0");
        }
    }
    else jTextField2.setText(jTextField2.getText() +"0");
    }                                       

    private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {                                        
    if (mulaiBaru){
        jTextField2.setText("0.");
        koma=true;
        mulaiBaru=false;
    }
    else{
        if (!koma){
            if (jTextField2.getText().isEmpty())jTextField2.setText("0.");
            else jTextField2.setText(jTextField2.getText() + ".");
            koma=true;
        }
    }
    }                                       

    private void jButton13ActionPerformed(java.awt.event.ActionEvent evt) {                                        
    nilai1 = Double.valueOf(jTextField2.getText());
    jTextField1.setText(jTextField2.getText() + " + " );
    mulaiBaru=true;
    proses=1;
    }                                       

    private void jButton14ActionPerformed(java.awt.event.ActionEvent evt) {                                        
    nilai1 = Double.valueOf(jTextField2.getText());
    jTextField1.setText(jTextField2.getText() + " - ");
    mulaiBaru=true;
    proses=2;
    }                                       

    private void jButton15ActionPerformed(java.awt.event.ActionEvent evt) {                                        
    nilai1 = Double.valueOf(jTextField2.getText());
    jTextField1.setText(jTextField2.getText() + " * ");
    mulaiBaru=true;
    proses=3;
    }                                       

    private void jButton16ActionPerformed(java.awt.event.ActionEvent evt) {                                        
    nilai1 = Double.valueOf(jTextField2.getText());
    jTextField1.setText(jTextField2.getText() + " / ");
    mulaiBaru=true;
    proses=4;
    }                                       

    private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {                                        
    nilai2 = Double.valueOf(jTextField2.getText());
    switch (proses){
        case 1:
            hasil = nilai1 + nilai2;
            break;
        case 2:
            hasil = nilai1 - nilai2;
            break;
        case 3:
            hasil = nilai1 * nilai2;
            break;
        case 4:
            hasil = nilai1 / nilai2;
            break;
        default:
        break;
    }
    if (hasil%1==0){
        hasilInt = (int)Math.round(hasil);
        jTextField2.setText(Integer.toString(hasilInt));
    }
    else{
        hasil = Double.valueOf(formatData.format(hasil));
        jTextField2.setText(Double.toString(hasil));
    }
    jTextField1.setText("");
    mulaiBaru=true;
    koma=false;
    nolLagi=true;
    proses=0;
    nilai1=0;
    nilai2=0;
    hasil=0;
  
    }                                       

    /**
     * @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.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);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Kalkulator().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                   
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton10;
    private javax.swing.JButton jButton11;
    private javax.swing.JButton jButton12;
    private javax.swing.JButton jButton13;
    private javax.swing.JButton jButton14;
    private javax.swing.JButton jButton15;
    private javax.swing.JButton jButton16;
    private javax.swing.JButton jButton17;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JButton jButton9;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration                 
}

0 komentar:

Posting Komentar

Site search

    Blogger news

    Blogroll

    About