. .

BAGIAN KE SEPULUH: Menulis Kode Pada Tombol Operator Math Di Java Form (GUI) Pada NetBeans IDE

Posted by CHRISTIANTO D,WIBOWO | D3MI-2016 STMIK BUMI GORA On Kamis, Januari 26, 2017 No comments

BAGIAN KE SEPULUH : Coding The Math Operator Button Of Java Calculator On Netbeans IDE, Sebelum kita meneruskan mempelajari pembelajaran ini, ada baiknya kita baca artikel-artikel sebelumnya yang terhubung dengan langkah-langkah pembelajaran ini (Link) BAHASA PEMROGRAMAN JAVA PADA NETBEANS IDE.
Big A Lot Of Gratitude To This Learning Resources:
Sebagai refrensi, kita bisa melihat JAVA NETBEANS IDE - MEMBUAT PROGRAM DAN ALGORITMA SERTA FLOWCHART KALKULATOR SEDERHANA, disana kita akan mendapat teori penjelasan singkat, semua langsung pada prakteknya dan kita dipersilahkan mengkopi java code-nya, dan disini kita akan mendapatkan penjelasan detailnya.

Masih dalam Project MyCalculator, dengan Package jCalculator, serta class yang disebut class JavaCalculator.java yang sudah kita buat sebelumnya. Kita sudah membuat dan memahami :
Dan sekarang kita menulis baris kode pada tombol-tombol operator math pada tombol Plus, Equals dan Clear pada form kalkulator agar Java program Calculator kita dapat berfungsi. 
Coding the Plus Button of our Java Calculator
Sekarang tombol Angka-angka sudah bekerja pada kalkulator Java, hal berikutnya yang harus dilakukan adalah untuk menangani tombol plus - Plus Button. Satu-satunya hal yang tombol plus perlu lakukan adalah untuk menyimpan apapun hasil penjumlahan saat ini di TextField. Angka pertama yang akan ditambahkan, kemudian akan merekam fakta bahwa itu adalah tombol plus yang sudah  diklik. 
Untuk menyimpan nilai, kita perlu mengatur field variabel, dan itu di luar kode variabel tombol. Hal ini dimaksudkan agar semua tombol dapat melihat apa yang telah disimpan di dalamnya. Tambahkan variabel berikut dekat bagian atas jendela coding: Kita dapat menempatkannya di dekat bagian bawah, dengan field variabel, NetBeans telah mengatur untuk Form objek, Tapi field variabel dan kode variabel tombol tetap harus terpisah;
private double total1 = 0.0;
Berikut jendela kodenya : 
package jCalculator;
/**
 *
 * @author CHRISTIANTO "GEMBLONG" DHARMA WIBOWO LEARN JAVA
 */
public class JavaCalculator extends javax.swing.JFrame {
    private double total1 = 0.00;

    /**
     * Creates new form JavaCalculator
     */
    public JavaCalculator() {
        initComponents();
    }
Kita sedang memempersiapkan sebuah variabel yang disebut total1. Jenis variabel adalah ganda. nilai default adalah 0,0.
Untuk menyimpan nilai dari TextField, kita perlu mendapatkan teks, tapi kita harus mengubahnya dari String ke Double. Kita dapat melakukannya dengan metode parseDouble dari objek ganda :
Double.parseDouble( txtDisplay.getText( ) )
Di antara kurung bulat parseDouble mendapatkan teks dari kolom teks txtDisplay. Namun, ketika kita menyimpan nilai dari TextField ke dalam variabel total1, kita perlu mempertahankan apa yang sudah ada di total1. Maka kita menghapus/clear kolom teks, dan mempersiapkan angka kedua.
Jadi kembali ke tampilan Design di NetBeans IDE. klik ganda tombol Plus untuk menghasilkan rintisan kode. Sekarang tambahkan dua baris kode berikut ke tombol plus tersebut :
  • total1 = total1 + Double.parseDouble( txtDisplay.getText( ) );
  • txtDisplay.setText("");
Di antara kurung bulat setText, kita memiliki sepasang tanda kutip ganda, dengan tidak ada ruang antara mereka. Hal ini cukup untuk menghapus kolom teks.
private void btnPlusActionPerformed(java.awt.event.ActionEvent evt) {                                      
        total1 = total1 + Double.parseDouble( txtDisplay.getText( ) );
        txtDisplay.setText("");
    } 
Dan untuk tombol Plus dapat berfungsi sudah kita berikan baris kodenya. Semua yang kita lakukan, adalah menyimpan angka dalam variabel total1, dan menjaganya. Setelah angka tersebut disimpan, kita sudah membersihkan TextField. User sekarang dapat memasukkan angka kedua yang akan ditambahkan ke angka pertama yang sudah disimpan.

Sampai disini kita belum bisa melihat proses operator Plus, karena kode untuk tombol Equals belum kita buat.

Coding the Equals Button of our Java Calculator
Setelah user telah mengklik angka kedua, tombol equals perlu diklik. Mengklik tombol equals akan menghasilkan jawaban dari proses operator penjumlahan/Plus.
Untuk menyimpan output dari perhitungan tersebut, kita dapat mengatur field variabel lain, untuk itu tambahkan baris kode berikut :
private double total2 = 0.0;
jendela kode akan terlihat seperti ini:
package jCalculator;
/**
 *
 * @author CHRISTIANTO "GEMBLONG" DHARMA WIBOWO LEARN JAVA
 */
public class JavaCalculator extends javax.swing.JFrame {
    private double total1 = 0.0;
    private double total2 = 0.0;
    /**
     * Creates new form JavaCalculator
     */
    public JavaCalculator() {
        initComponents();
    }
Untuk mendapatkan jawaban untuk perhitungan, kita akan mengambil apa pun hasil proses perhitungan yang saat ini masih disimpan di total1 dan menambahkannya ke apa yang ada saat ini di TextField. Sekali lagi, meskipun, kita perlu mengurai string dari TextField dan mengubahnya menjadi double. Maka kita kembali ke tampilan Design dan double klik tombol equals dan dalam kode rintisan yang dibuat, tambahkan baris kode berikut:
total2 = total1 + Double.parseDouble( txtDisplay.getText( ) );
baris kode ini mendapat teks dari kolom teks dan mengkonversi string ke double. Hasilnya kemudian ditambahkan ke total1. Jawabannya kemudian disimpan dalam variabel total2.

Hal berikutnya yang perlu kita lakukan adalah untuk menampilkan jawaban untuk hasil perhitungan kembali ke TextField. Namun, sekarang kita harus mengkonversi double kembali ke dalam string, seperti TextField menahan teks dan bukan angka. Jika kita mencoba untuk menyimpan nilai double langsung ke dalam kolom teks kita akan mendapatkan error.
Untuk mengkonversi double menjadi teks kita dapat menggunakan metode toString dari objek double. Tambahkan baris kode berikut :
private void btnEqualsActionPerformed(java.awt.event.ActionEvent evt) {                                      
        total2 = total1 + Double.parseDouble( txtDisplay.getText( ) );
        txtDisplay.setText( Double.toString( total2) );
    }
konversi dilakukan antara kurung bulat setText. Tapi kita bisa mengatur variabel baru, jika memang kita inginkan :
private void btnEqualsActionPerformed(java.awt.event.ActionEvent evt) {                                    
       String s1 = Double.toString( total2 );
                txtDisplay.setText( s1 );
    }
Tapi hasilnya sama: mengkonversi double menjadi string.
Baris kode terakhir untuk tombol equals dapat menghapus variabel total1. Setelah variabel total1 dibersihkan, penghitungan baru dapat dimasukkan. Berikut baris kode  tambahannya:
total1 = 0;
Tiga baris kode untuk tombol Equals sekarang seperti ini:
private void btnEqualsActionPerformed(java.awt.event.ActionEvent evt) {                                      
        total2 = total1 + Double.parseDouble( txtDisplay.getText( ) ) ;
        txtDisplay.setText( Double.toString(total2) );
        total1 = 0;
    }
Pada bagian berikutnya yang belum kita berikan baris kode adalah tombol Clear untuk membersihkan semua hasil proses.

Coding the Clear Button of our Java Calculator
Satu-satunya hal yang tersisa untuk penulisan baris kode adalah untuk tombol Clear. Kita perlu untuk menghapus variabel total2 dan mengatur teks dalam TextField ke string kosong.
Dalam Design view, klik ganda tombol Clear untuk membuat rintisan kode dan kemudian tambahkan dua baris kode ini :
  • total2 = 0;
  • txtDisplay.setText("");
Setelah kita menambahkan 2 baris kode diatas,
private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {                                      
        total2 = 0;
        txtDisplay.setText("");
    }

Sekarang kita dapat mencoba Java program kalkulator-nya. Jalankan program dan coba tambahkan angka. Java program harus menemukan bahwa kalkulator kita memproses operator penjumlahan dengan baik.


Something Miss !!!
Ada satu hal yang hilang, - simbol tombol titik. Pada saat ini, kalkulator kita tidak bisa menangani penjumlahan seperti 23,6 + 35,8. Jadi menambahkan tombol titik untuk form kita. Kita harus menulis kode untuk itu Tombol Point. Prosesnya sama saja dengan tombol-tombol angka;

  1. Tab View Design
  2. Tambahkan Button/tombol dan atur posisinya dalam jPanel1 (jPanel2 untuk Operator)
  3. Rubah variable name nya menjadi btnPoint
  4. Rubah teks nya menjadi simbol "."/titik
  5. Buat Events ⇒ Action ⇒ actionPerformed; untuk membuat rintisan baris kode
Maka barisan kode untuk btnPoint menjadi :
private void btnPointActionPerformed(java.awt.event.ActionEvent evt) {                                      
        String btnPointText = txtDisplay.getText() + btnPoint.getText();
        txtDisplay.setText( btnPointText );
    }
Dan kalkulator penjumlahan kita mampu untuk mengoperasikan penjumlahan 23,6 + 35,8 = 59.4




All Source Code :
package jCalculator;
/**
 *
 * @author CHRISTIANTO "GEMBLONG" DHARMA WIBOWO LEARN JAVA
 */
public class JavaCalculator extends javax.swing.JFrame {
    private double total1 = 0.0;
    private double total2 = 0.0;
    /**
     * Creates new form JavaCalculator
     */
    public JavaCalculator() {
        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">                        
    private void initComponents() {

        txtDisplay = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        btnOne = new javax.swing.JButton();
        btnTwo = new javax.swing.JButton();
        btnThree = new javax.swing.JButton();
        btnFour = new javax.swing.JButton();
        btnFive = new javax.swing.JButton();
        btnSix = new javax.swing.JButton();
        btnSeven = new javax.swing.JButton();
        btnEight = new javax.swing.JButton();
        btnNine = new javax.swing.JButton();
        btnZero = new javax.swing.JButton();
        btnPoint = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        btnPlus = new javax.swing.JButton();
        btnEquals = new javax.swing.JButton();
        btnClear = new javax.swing.JButton();
        labelImage = new javax.swing.JLabel();

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

        txtDisplay.setFont(new java.awt.Font("Arial Black", 0, 18)); // NOI18N

        btnOne.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        btnOne.setText("1");
        btnOne.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOneActionPerformed(evt);
            }
        });

        btnTwo.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        btnTwo.setText("2");
        btnTwo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTwoActionPerformed(evt);
            }
        });

        btnThree.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        btnThree.setText("3");
        btnThree.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThreeActionPerformed(evt);
            }
        });

        btnFour.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        btnFour.setText("4");
        btnFour.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFourActionPerformed(evt);
            }
        });

        btnFive.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        btnFive.setText("5");
        btnFive.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFiveActionPerformed(evt);
            }
        });

        btnSix.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        btnSix.setText("6");
        btnSix.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSixActionPerformed(evt);
            }
        });

        btnSeven.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        btnSeven.setText("7");
        btnSeven.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSevenActionPerformed(evt);
            }
        });

        btnEight.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        btnEight.setText("8");
        btnEight.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEightActionPerformed(evt);
            }
        });

        btnNine.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        btnNine.setText("9");
        btnNine.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNineActionPerformed(evt);
            }
        });

        btnZero.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        btnZero.setText("0");
        btnZero.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnZeroActionPerformed(evt);
            }
        });

        btnPoint.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        btnPoint.setText(".");
        btnPoint.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnPoint.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPointActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(btnOne, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnTwo, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(28, 28, 28)
                        .addComponent(btnThree, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(btnFour, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnFive, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnSix, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(btnSeven, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(btnEight, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(btnZero, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addComponent(btnPoint, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(1, 1, 1))
                            .addComponent(btnNine, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnTwo, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnThree, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnOne, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnFour, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnFive, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSix, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSeven, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnEight, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnNine, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnZero, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnPoint, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 16, Short.MAX_VALUE))
        );

        btnPlus.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        btnPlus.setText("+");
        btnPlus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPlusActionPerformed(evt);
            }
        });

        btnEquals.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        btnEquals.setText("=");
        btnEquals.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEqualsActionPerformed(evt);
            }
        });

        btnClear.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        btnClear.setText("Clear");
        btnClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnClearActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnClear, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(9, 9, 9)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(btnEquals, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnPlus, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnPlus, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnEquals, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnClear, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        labelImage.setIcon(new javax.swing.ImageIcon("D:\\@ WEB PROJECT\\@YU ERL IE\\JAVA N NETBEANS\\MyCalculator\\bumigora.png")); // NOI18N
        labelImage.setText("jLabel2");

        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(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtDisplay)
                    .addComponent(jPanel1, 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)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelImage, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(14, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtDisplay, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelImage))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        pack();
    }// </editor-fold>                      
    private void btnOneActionPerformed(java.awt.event.ActionEvent evt) {                                      
        String btnOneText = txtDisplay.getText() + btnOne.getText();
        txtDisplay.setText(btnOneText);
    }                                    

    private void btnTwoActionPerformed(java.awt.event.ActionEvent evt) {                                      
        String btnTwoText = txtDisplay.getText() + btnTwo.getText();
        txtDisplay.setText( btnTwoText );
    }                                    

    private void btnThreeActionPerformed(java.awt.event.ActionEvent evt) {                                        
        String btnThreeText = txtDisplay.getText() + btnThree.getText();
        txtDisplay.setText( btnThreeText );
    }                                      

    private void btnFourActionPerformed(java.awt.event.ActionEvent evt) {                                      
        String btnFourText = txtDisplay.getText() + btnFour.getText();
        txtDisplay.setText( btnFourText );
    }                                      

    private void btnFiveActionPerformed(java.awt.event.ActionEvent evt) {                                      
        String btnFiveText = txtDisplay.getText() + btnFive.getText();
        txtDisplay.setText( btnFiveText );
    }                                      

    private void btnSixActionPerformed(java.awt.event.ActionEvent evt) {                                      
        String btnSixText = txtDisplay.getText() + btnSix.getText();
        txtDisplay.setText( btnSixText );
    }                                    

    private void btnPointActionPerformed(java.awt.event.ActionEvent evt) {                                        
        String btnPointText = txtDisplay.getText() + btnPoint.getText();
        txtDisplay.setText( btnPointText );
    }                                      

    private void btnSevenActionPerformed(java.awt.event.ActionEvent evt){
        String btnSevenText = txtDisplay.getText() + btnSeven.getText();
        txtDisplay.setText( btnSevenText );
    }
   
    private void btnEightActionPerformed(java.awt.event.ActionEvent evt){
        String btnEightText = txtDisplay.getText() + btnEight.getText();
        txtDisplay.setText( btnEightText );
    }
   
    private void btnNineActionPerformed(java.awt.event.ActionEvent evt){
        String btnNineText = txtDisplay.getText() + btnNine.getText();
        txtDisplay.setText( btnNineText );
    }
   
    private void btnZeroActionPerformed(java.awt.event.ActionEvent evt){
        String btnZeroText = txtDisplay.getText() + btnZero.getText();
        txtDisplay.setText( btnZeroText );
    }
   
    private void btnPlusActionPerformed(java.awt.event.ActionEvent evt) {                                      
        total1 = total1 + Double.parseDouble( txtDisplay.getText( ) );
        txtDisplay.setText("");
    }
   
    private void btnEqualsActionPerformed(java.awt.event.ActionEvent evt) {                                      
        total2 = total1 + Double.parseDouble( txtDisplay.getText( ) ) ;
        txtDisplay.setText( Double.toString(total2) );
        total1 = 0;
    }
   
    private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {                                      
        total2 = 0;
        txtDisplay.setText("");
    }
   
    /**
     * @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 | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JavaCalculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
       
        //</editor-fold>

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

    // Variables declaration - do not modify                    
    private javax.swing.JButton btnClear;
    private javax.swing.JButton btnEight;
    private javax.swing.JButton btnEquals;
    private javax.swing.JButton btnFive;
    private javax.swing.JButton btnFour;
    private javax.swing.JButton btnNine;
    private javax.swing.JButton btnOne;
    private javax.swing.JButton btnPlus;
    private javax.swing.JButton btnPoint;
    private javax.swing.JButton btnSeven;
    private javax.swing.JButton btnSix;
    private javax.swing.JButton btnThree;
    private javax.swing.JButton btnTwo;
    private javax.swing.JButton btnZero;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel labelImage;
    private javax.swing.JTextField txtDisplay;
    // End of variables declaration                  
}

Pada bagian pembelajaran kita berikutnya adalah menambahkan fungsi operator math Multiply/perkalian, Divide/pembagian dan Subtract/pengurangan - Subtract, Divide, Multiply Buttons 

0 komentar:

Posting Komentar