Langsung ke konten utama

Syntax Kalkulator satu TextField

package javaapplication76;
public class Kalkulator extends javax.swing.JFrame {

    private String dt1 = "";
    private String dt2 = "";
    private int data1 = 0;
    private int data2 = 0;
    private int hasil = 0;
    private int plusop = 0;
    private int multiop = 0;
    private String dtdummy = "";
    private int opr = 0;
    private String opraksi = "";
   
    public Kalkulator() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                         
    private void initComponents() {

        edtJumlah = new javax.swing.JTextField();
        btn1 = new javax.swing.JButton();
        btn2 = new javax.swing.JButton();
        btn3 = new javax.swing.JButton();
        btn4 = new javax.swing.JButton();
        btn5 = new javax.swing.JButton();
        btn6 = new javax.swing.JButton();
        btnTambah = new javax.swing.JButton();
        btnKali = new javax.swing.JButton();
        btnHasil = new javax.swing.JButton();
        btnreset = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

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

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

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

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

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

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

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

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

        btnHasil.setText("Hasil");
        btnHasil.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnHasilActionPerformed(evt);
            }
        });

        btnreset.setText("reset");
        btnreset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnresetActionPerformed(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()
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btn1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btn2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btn3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnTambah))
                    .addComponent(edtJumlah, javax.swing.GroupLayout.PREFERRED_SIZE, 176, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(btnHasil, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addComponent(btn4)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(btn5)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(btn6)
                            .addGap(5, 5, 5)
                            .addComponent(btnKali))
                        .addComponent(btnreset, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap(26, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(edtJumlah, 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(btn3)
                    .addComponent(btn2)
                    .addComponent(btn1)
                    .addComponent(btnTambah))
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btn6)
                    .addComponent(btn5)
                    .addComponent(btn4)
                    .addComponent(btnKali))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnHasil, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnreset, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(23, Short.MAX_VALUE))
        );

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

    private void btn1ActionPerformed(java.awt.event.ActionEvent evt) {                                    
        // TODO add your handling code here:
        if (this.opr == 0) {
            this.dt1 = this.dt1 + " 1";
            edtJumlah.setText(dt1);
            this.data1 = Integer.parseInt(dt1);                  
        } else if (this.opr > 0) {
            this.dt2 = this.dt2 + " 1";
            if (this.opraksi.equalsIgnoreCase(" plus")) {
                edtJumlah.setText(dt1 + " + " + dt2);
            } else if (this.opraksi.equalsIgnoreCase("kali")) {
                edtJumlah.setText(dt1 + " * " + dt2);
            }
            this.data2 = Integer.parseInt(dt2);
        }
    }                                   

    private void btn2ActionPerformed(java.awt.event.ActionEvent evt) {                                    
         if (this.opr == 0) {
            this.dt1 = this.dt1 + " 2";
            edtJumlah.setText(dt1);
            this.data1 = Integer.parseInt(dt1);                  
        } else if (this.opr > 0) {
            this.dt2 = this.dt2 + " 2";
            if (this.opraksi.equalsIgnoreCase(" plus")) {
                edtJumlah.setText(dt1 + " + " + dt2);
            } else if (this.opraksi.equalsIgnoreCase("kali")) {
                edtJumlah.setText(dt1 + " * " + dt2);
            }
            this.data2 = Integer.parseInt(dt2);
        }
    }                                   

    private void btn3ActionPerformed(java.awt.event.ActionEvent evt) {                                    
         if (this.opr == 0) {
            this.dt1 = this.dt1 + " 3";
            edtJumlah.setText(dt1);
            this.data1 = Integer.parseInt(dt1);                  
        } else if (this.opr > 0) {
            this.dt2 = this.dt2 + " 3";
            if (this.opraksi.equalsIgnoreCase(" plus")) {
                edtJumlah.setText(dt1 + " + " + dt2);
            } else if (this.opraksi.equalsIgnoreCase("kali")) {
                edtJumlah.setText(dt1 + " * " + dt2);
            }
            this.data2 = Integer.parseInt(dt2);
        }
    }                                   

    private void btn4ActionPerformed(java.awt.event.ActionEvent evt) {                                    
        if (this.opr == 0) {
            this.dt1 = this.dt1 + " 4";
            edtJumlah.setText(dt1);
            this.data1 = Integer.parseInt(dt1);                  
        } else if (this.opr > 0) {
            this.dt2 = this.dt2 + " 4";
            if (this.opraksi.equalsIgnoreCase(" plus")) {
                edtJumlah.setText(dt1 + " + " + dt2);
            } else if (this.opraksi.equalsIgnoreCase("kali")) {
                edtJumlah.setText(dt1 + " * " + dt2);
            }
            this.data2 = Integer.parseInt(dt2);
        }
    }                                   

    private void btn5ActionPerformed(java.awt.event.ActionEvent evt) {                                    
        if (this.opr == 0) {
            this.dt1 = this.dt1 + " 5";
            edtJumlah.setText(dt1);
            this.data1 = Integer.parseInt(dt1);                  
        } else if (this.opr > 0) {
            this.dt2 = this.dt2 + " 5";
            if (this.opraksi.equalsIgnoreCase(" plus")) {
                edtJumlah.setText(dt1 + " + " + dt2);
            } else if (this.opraksi.equalsIgnoreCase("kali")) {
                edtJumlah.setText(dt1 + " * " + dt2);
            }
            this.data2 = Integer.parseInt(dt2);
        }
    }                                   

    private void btn6ActionPerformed(java.awt.event.ActionEvent evt) {                                    
        if (this.opr == 0) {
            this.dt1 = this.dt1 + " 6";
            edtJumlah.setText(dt1);
            this.data1 = Integer.parseInt(dt1);                  
        } else if (this.opr > 0) {
            this.dt2 = this.dt2 + " 6";
            if (this.opraksi.equalsIgnoreCase(" plus")) {
                edtJumlah.setText(dt1 + " + " + dt2);
            } else if (this.opraksi.equalsIgnoreCase("kali")) {
                edtJumlah.setText(dt1 + " * " + dt2);
            }
            this.data2 = Integer.parseInt(dt2);
        }
    }                                   

    private void btnTambahActionPerformed(java.awt.event.ActionEvent evt) {                                         
        this.opraksi = " plus";
        this.opr++;
    }                                        

    private void btnKaliActionPerformed(java.awt.event.ActionEvent evt) {                                       
        this.opraksi = "kali";
        this.opr++;
    }                                      

    private void btnHasilActionPerformed(java.awt.event.ActionEvent evt) {                                        
        if (opraksi.equals(" plus")) {
            //String dt1 = Integer.parseInt(dt1);
            hasil = Integer.parseInt(dt1) * Integer.parseInt(dt2);
            edtJumlah.setText(dt1+" * "+dt2+ " = "+hasil );
        } if (opraksi.equals(" kali")) {
            hasil = Integer.parseInt(dt1) + Integer.parseInt(dt2);
            edtJumlah.setText(dt1+ " + "+dt2+ " = "+hasil);
        } else
            System.out.println("ERROR");
    }                                       

    private void btnresetActionPerformed(java.awt.event.ActionEvent evt) {                                        
        edtJumlah.setText(" ");
        this.dt1 = "";
        this.dt2 = "";
        this.dtdummy = "";
        this.data1 = 0;
        this.data2 = 0;
        this.opr = 0;
        this.opraksi = "";
    }                                       

    /**
     * @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 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 btnHasil;
    private javax.swing.JButton btnKali;
    private javax.swing.JButton btnTambah;
    private javax.swing.JButton btnreset;
    private javax.swing.JTextField edtJumlah;
    // End of variables declaration                  
}

Komentar