. .

BAGIAN KE SEPULUH:Kode Operator Tombol Math Subtract, Divide, Multiply Di Java Form (GUI) Pada NetBeans IDE

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

Coding The Subtract, Divide, Multiply Buttons For Java Calculator In 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 Subtract, Divide, dan  Multiply pada form kalkulator Java program Calculator.
Sekarang tombol plus sudah bekerja pada Kalkulator Java, kita dapat menambahkan tombol untuk mengurangi/subtract, membagi/divide dan mengalikan/multiply. Sama seperti tombol lainnya yang, tidak akan melakukan perhitungan apapun tanpa code control didalamnya. Hal pertama yang harus dilakukan adalah untuk menempatkan beberapa tombol pada form kalkulator. Jangan ragu untuk mencoba dengan desain kita sendiri, seperti ini contohnya, atau terserah kita mendesignya.
Atau seperti design saya ini :

Atau kita akan design ulang bentuknya nanti saja, itu perkara mudah !
Setelah menambahkan tombol baru, mengubah nama variabel default menjadi btnSubtract, btnDivide, dan btnMultiply, seperti yang kita lakukan sebelumnya sewaktu menambahkan tombol "."/titik btnPoint.
BAGIAN KE SEPULUH: Menulis Kode Pada Tombol Operator Math Di Java Form (GUI) Pada NetBeans IDE 
Teknik yang akan kita gunakan untuk mendapatkan yang tombol Operator diklik adalah untuk menyimpan tombol teks dalam field variabel. Kita kemudian dapat menggunakan pernyataan switch untuk memeriksa yang karakter dalam field variabel.
  • if itu simbol "+" kita melakukan proses addition/penjumlahan;
  • if itu Simbol "-" kita melakukan proses subtraction/pengurangan; 
  • if itu adalah simbol "/" "+" kita melakukan proses division/pembagian; 
  • if itu simbol "*" kita melakukan proses multiply/perkalian.
Jadi klik tombol Source untuk kembali ke kode kita, tambahkan variabel bidang berikut :
private char math_operator;
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;
    private char math_operator;
    /**
     * Creates new form JavaCalculator
     */
    public JavaCalculator() {
        initComponents();
    }
Kita dapat mengatur metode untuk mendapatkan karakter pada tombol yang diklik. Menambahkan metode berikut ke kode kita:
private void getOperator(String btnText){
        math_operator=btnText.charAt(0);
        total1=total1+Double.parseDouble(txtDisplay.getText());
        txtDisplay.setText("");
    }
Kita dapat menambahkan metode di atas di mana saja di kode kita, selama itu diantara kurung kurawa dari class, dan bukan antara kurung kurawa dari metode lainnya.
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;
    private char math_operator;
    private void getOperator(String btnText){
        math_operator=btnText.charAt(0);
        total1=total1+Double.parseDouble(txtDisplay.getText());
        txtDisplay.setText("");
    }
    /**
     * Creates new form JavaCalculator
     */
    public JavaCalculator() {
        initComponents();
    }
Kita telah membuat metode getOperator. Ini adalah metode kekosongan, jadi tidak akan mengembalikan nilai apapun - itu hanya akan melanjutkan eksekusi kode. Di antara kurung bulat dari metode di header, kita memiliki variabel String disebut btnText. Ini jelas teks dari tombol yang diklik - kita pilih.

Teks dari tombol adalah string. Namun, switch statements di Java tidak bisa menangani string, jadi kita perlu mengkonversi string ke karakter, jadi kita perlu melakukannya dengan baris kode seperti ini:
math_operator = btnText.charAt(0);
Metode string charAt  akan mendapatkan karakter dari string. Karakter yang berada diantara kurung bulat charAt - btnText.charAt(0);. Simbol matematika dari tombol-tombol akan selalu berada di karakter 0 dalam string. Hal ini kemudian disimpan dalam char field variabel yang baru saja dibuat di atas kode.

Perhatikan dua baris kode lainnya,
        total1=total1+Double.parseDouble(txtDisplay.getText());
        txtDisplay.setText("");
kode-kode tersebut adalah persis sama dengan garis dari tombol plus, melakukan proses yang sama - menyimpan nomor pertama dalam variabel yang disebut total1. Setiap tombol Operator perlu melakukan hal ini, sehingga masuk akal untuk memiliki dua baris dalam metode, bukan di kode tombol operator. Jadi temukan kode btnPlus dan hapus dua baris berikut yang sudah ada sebelumnya :
  • total1 = total1 + Double.parseDouble(txtDisplay.getText( ) );
  • txtDisplay.setText("");
Dan ganti dengan ini :
  • String button_text = btnPlus.getText();
  • getOperator(button_text);

Baris pertama -  String button_text = btnPlus.getText();  untuk mendapat teks dari tombol plus dan menyimpannya dalam variabel string. Hal ini kemudian diserahkan kepada metode getOperator .

Dua baris yang sama dapat ditambahkan ke tombol operator lain, hanya mengubah nama tombol. Kembali ke tampilan Design dan double klik tombol "-"/Divide/Pengurangan. Untuk membuat rintisan kode, dan tambahkan kode berikut :
  • String button_text = btnSubtract.getText();
  • getOperator(button_text);
Meskipun kita telah menggunakan nama yang sama untuk String variabel, Java tidak akan bingung, karena setiap lokal button_text diperuntukkan untuk kode tombol tertentu. Klik ganda tombol membagi dan tambahkan baris kode ini:
  • String button_text = btnDivide.getText();
  • getOperator(button_text);
Dan baris kode untuk tombol multiply:
  • String button_text = btnMultiply.getText();
  • getOperator(button_text);
Sekarang bahwa kita memiliki kode untuk semua tombol empat operator, dan juga harus membuat perubahan kode untuk tombol equals. Untuk tombol equals, kita dapat mengatur switch statement untuk memeriksa proses apa yang ada di math_operator variable.
switch ( math_operator ) {
     case '+':
break;
     case '-':
break;
    case '/':
break;
    case '*':
break;
}

switch statement memiliki kasus untuk masing-masing operator matematika: +, -, /, dan *. Kita belum menambahkan kode apapun, Tapi kita lihat kode kita sudah memiliki kode sendiri untuk tombol Equals:
  • total2 = total1 + Double.parseDouble( txtDisplay.getText( ) ) ;
  • txtDisplay.setText( Double.toString(total2) );
  • total1 = 0;
Dua baris terakhir masih bisa kita gunakan, dan tidak perlu diubah. Sedangkan baris pertama dapat digunakan dalam switch statement. ini dapat digunakan sebagai kode untuk masing-masing case :
switch ( math_operator ) {
     case '+': total2 = total1 + Double.parseDouble(txtDisplay.getText( ) );

break;
     case '-': total2 = total1 - Double.parseDouble(txtDisplay.getText( ) );
break;
    case '/': total2 = total1 / Double.parseDouble(txtDisplay.getText( ) );
break;
    case '*': total2 = total1 * Double.parseDouble(txtDisplay.getText( ) );
break;
}

txtDisplay.setText( Double.toString(total2) );

total1 = 0;

Setelah kita menambahkan kode baru untuk tombol equals, jalankan java program kalkulator-nya dan mencobanya. Coba perhitungan berikut, untuk melihat apakah ia bekerja :
  1. 58. 6 + 37.5 = 96.1
  2. 78 - 25.5 = 52.5
  3. 68 / 8 = 8.5
  4. 56.3 * 6.8 = 382.84
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;
    private char math_operator;
    private void getOperator(String btnText){
        math_operator=btnText.charAt(0);
        total1=total1+Double.parseDouble(txtDisplay.getText());
        txtDisplay.setText("");
    }
    /**
     * 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();
        labelImage = new javax.swing.JLabel();
        btnOne = new javax.swing.JButton();
        btnTwo = new javax.swing.JButton();
        btnThree = new javax.swing.JButton();
        btnSix = new javax.swing.JButton();
        btnFive = new javax.swing.JButton();
        btnFour = new javax.swing.JButton();
        btnSeven = new javax.swing.JButton();
        btnEight = new javax.swing.JButton();
        btnZero = new javax.swing.JButton();
        btnClear = new javax.swing.JButton();
        btnPoint = new javax.swing.JButton();
        btnNine = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        btnPlus = new javax.swing.JButton();
        btnSubtract = new javax.swing.JButton();
        btnDivide = new javax.swing.JButton();
        btnMultiply = new javax.swing.JButton();
        btnEquals = new javax.swing.JButton();

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

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

        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

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

        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);
            }
        });

        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);
            }
        });

        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);
            }
        });

        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);
            }
        });

        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);
            }
        });

        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);
            }
        });

        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);
            }
        });

        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);
            }
        });

        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);
            }
        });

        btnPlus.setFont(new java.awt.Font("Arial Black", 1, 14)); // NOI18N
        btnPlus.setText("+");
        btnPlus.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnPlus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPlusActionPerformed(evt);
            }
        });

        btnSubtract.setFont(new java.awt.Font("Arial Black", 1, 14)); // NOI18N
        btnSubtract.setText("-");
        btnSubtract.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnSubtract.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSubtractActionPerformed(evt);
            }
        });

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

        btnMultiply.setFont(new java.awt.Font("Arial Black", 1, 14)); // NOI18N
        btnMultiply.setText("*");
        btnMultiply.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnMultiply.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMultiplyActionPerformed(evt);
            }
        });

        btnEquals.setFont(new java.awt.Font("Arial Black", 1, 14)); // NOI18N
        btnEquals.setText("=");
        btnEquals.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEqualsActionPerformed(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)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(btnPlus, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnDivide, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(btnSubtract, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnMultiply, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(btnEquals, javax.swing.GroupLayout.PREFERRED_SIZE, 146, 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()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnPlus, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnDivide, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSubtract, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnMultiply, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnEquals, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        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)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(278, 278, 278))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtDisplay)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.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(16, 16, 16)
                                .addComponent(btnThree, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.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)
                                .addGap(16, 16, 16)
                                .addComponent(btnSix, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.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)
                                .addGap(16, 16, 16)
                                .addComponent(btnNine, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnZero, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(7, 7, 7)
                                .addComponent(btnClear, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnPoint, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(33, 33, 33)
                        .addComponent(labelImage, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(5, 5, 5))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(txtDisplay, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(30, 30, 30)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(btnOne, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .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(labelImage))
                        .addGap(10, 10, 10)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .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))
                        .addGap(10, 10, 10)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .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))
                        .addGap(13, 13, 13)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btnZero, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(btnClear, 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(2, 2, 2))
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(5, 5, 5)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        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 btnSubtractActionPerformed(java.awt.event.ActionEvent evt) {                                          
        String button_text = btnSubtract.getText();
        getOperator(button_text);
    }                                          

    private void btnDivideActionPerformed(java.awt.event.ActionEvent evt) {                                        
        String button_text = btnDivide.getText();
        getOperator(button_text);
    }                                        

    private void btnMultiplyActionPerformed(java.awt.event.ActionEvent evt) {                                          
        String button_text = btnMultiply.getText();
        getOperator(button_text);
    }                                          

    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) {                                      
        String button_text = btnPlus.getText();
        getOperator(button_text);
    }
   
    private void btnEqualsActionPerformed(java.awt.event.ActionEvent evt) {                                      
        switch ( math_operator ) {
           case '+': total2 = total1 + Double.parseDouble(txtDisplay.getText( ) );
           break;
           case '-': total2 = total1 - Double.parseDouble(txtDisplay.getText( ) );
           break;
           case '/': total2 = total1 / Double.parseDouble(txtDisplay.getText( ) );
           break;
           case '*': total2 = total1 * Double.parseDouble(txtDisplay.getText( ) );
           break;
        }
    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 btnDivide;
    private javax.swing.JButton btnEight;
    private javax.swing.JButton btnEquals;
    private javax.swing.JButton btnFive;
    private javax.swing.JButton btnFour;
    private javax.swing.JButton btnMultiply;
    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 btnSubtract;
    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                  
}
Download all Java Program MyCalculator from Google Drive !!!
Dan kita sekarang memiliki kalkulator kerja sederhana yang dapat menambah, mengurangi, membagi, dan mengalikan.  Sekarang kita sudah memiliki beberapa latihan dengan objek form, mari kita membuat program baru yang menggunakan kontrol yang lebih umum yang akan kita temukan pada Java form. - Java Combo Boxes -

0 komentar:

Posting Komentar