.syntaxhighlighter .line .content .block { background: none !important; }

Aplikasi Kalkulator Sederhana

Saturday, May 9, 2009

Kalkulator sederhana ini terdiri dari fungsi-fungsi perhitungan standar, seperti +, - , *, /, tombol Clear, tombol BackSpace dan tombol Exit. Object yang terdapat pada Jframe ini adalah :

  1. Panel
  2. TextField
  3. Button
Langkah-langkah desain Form:
  1. Desain Form seperti gambar diatas
  2. Edit teks pada masing-masing objek, klik kanan pada objek - pilih Edit Text.
  3. Ganti nama masing-masing objek, klik kanan pada objek - pilih Change Variabel Name...contoh untuk TextField ganti jadi layarTextField. untuk Button ganti jadi satuButton, dst...
  4. Jika perlu ganti properti warna Panel seperti gambar diatas.
Langkah berikutnya adalah menuliskan kode programnya untuk masing-masing Button. Untuk menuliskan kode program pada objek, klik kanan pada objek - pilih menu Events - Action - actionPerformed. Berikut ini kode program untuk button angka :
private void satuButtonActionPerformed(java.awt.event.ActionEvent evt) {                                          
       angka += "1";
       layarTextField.setText(angka);
   }
Lanjutkan untuk tombol angka berikutnya. Untuk tombol desimal (.) berikut kode programnya:
private void komaButtonActionPerformed(java.awt.event.ActionEvent evt) {                                          
       if(layarTextField.getText().length()<= 1){
           angka = "0.";
           layarTextField.setText(angka);
       }else{
           angka += ".";
           layarTextField.setText(angka);}
   }
Untuk tombol Tambah(+):
private void tambahButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
       operator = "+";
       operand1 = Double.parseDouble(layarTextField.getText());
       angka = "";
   }
Lanjutkan untuk tombol kurang, kali dan bagi. Kode untuk tombol samadengan(=):
private void samadenganButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
   operand2 = Double.parseDouble(layarTextField.getText());

   if(operator == "+"){
       hasil = operand1 + operand2;
   }else if(operator == "-"){
       hasil = operand1 - operand2;
   }else if(operator == "*"){
       hasil = operand1 * operand2;
   }else if(operator == "/"){
       if(operand2 == 0){
           hasil = 0;
       }else{
           hasil = operand1 / operand2;}
   }else{
       hasil = (operand1 / operand2) * 100;
   }

   layarTextField.setText(String.valueOf(hasil));
   angka="";
}
Untuk tombol Clear:
private void clearButtonActionPerformed(java.awt.event.ActionEvent evt) {                                       
    angka = "";
    layarTextField.setText(angka);
    layarTextField.setText("0");
}
Untuk tombol BackSpace(B):
private void bButtonActionPerformed(java.awt.event.ActionEvent evt) {                                       
       if(layarTextField.getText().length() <= 1)    {
           angka = "";
           layarTextField.setText("0");
       }else{
           angka = layarTextField.getText().substring(0, layarTextField.getText().length() - 1);
           layarTextField.setText(angka);
       }
   }
Untuk tombol Exit(E):
private void keluarMenuItemActionPerformed(java.awt.event.ActionEvent evt) {                                             
       System.exit(0);
   }
Setelah selesai menuliskan kode program, langkah berikutnya menjalankan Form, tekan Shift+F6.

0 komentar:

About This Blog

This blog published for the beginners who learn Java.

  © Blogger template Columnus by Ourblogtemplates.com 2008

Back to TOP