Display :
Design :
Source code :
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author DesDesianny
*/
public class TampilanKalkulator extends javax.swing.JFrame {
String angka;
double jumlah,angka1,angka2;
int pilih;
public TampilanKalkulator() {
initComponents();
angka=" ";
display.setText(angka);
}
/**
* 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() {
display = new javax.swing.JTextField();
btnHapus = new javax.swing.JButton();
btnSamadengan = new javax.swing.JButton();
btnAngka1 = new javax.swing.JButton();
btnAngka2 = new javax.swing.JButton();
btnAngka3 = new javax.swing.JButton();
btnAngka4 = new javax.swing.JButton();
btnAngka5 = new javax.swing.JButton();
btnAngka6 = new javax.swing.JButton();
btnAngka7 = new javax.swing.JButton();
btnAngka8 = new javax.swing.JButton();
btnAngka9 = new javax.swing.JButton();
btnAngka0 = new javax.swing.JButton();
btnTambah = new javax.swing.JButton();
btnKurang = new javax.swing.JButton();
btnKali = new javax.swing.JButton();
btnBagi = new javax.swing.JButton();
btnKoma = new javax.swing.JButton();
judul = new javax.swing.JLabel();
nama = new javax.swing.JLabel();
btnSin = new javax.swing.JButton();
btnCos = new javax.swing.JButton();
btnTan = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setBackground(new java.awt.Color(255, 255, 255));
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
display.setEditable(false);
display.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
display.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
displayActionPerformed(evt);
}
});
getContentPane().add(display, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 40, 340, 40));
btnHapus.setText("C");
btnHapus.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnHapusActionPerformed(evt);
}
});
getContentPane().add(btnHapus, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 93, 190, 30));
btnSamadengan.setText("=");
btnSamadengan.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSamadenganActionPerformed(evt);
}
});
getContentPane().add(btnSamadengan, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 93, 130, 30));
btnAngka1.setText("1");
btnAngka1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAngka1ActionPerformed(evt);
}
});
getContentPane().add(btnAngka1, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 130, 70, 30));
btnAngka2.setText("2");
btnAngka2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAngka2ActionPerformed(evt);
}
});
getContentPane().add(btnAngka2, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 130, 70, 30));
btnAngka3.setText("3");
btnAngka3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAngka3ActionPerformed(evt);
}
});
getContentPane().add(btnAngka3, new org.netbeans.lib.awtextra.AbsoluteConstraints(190, 130, 70, 30));
btnAngka4.setText("4");
btnAngka4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAngka4ActionPerformed(evt);
}
});
getContentPane().add(btnAngka4, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 170, 70, 30));
btnAngka5.setText("5");
btnAngka5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAngka5ActionPerformed(evt);
}
});
getContentPane().add(btnAngka5, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 170, 70, 30));
btnAngka6.setText("6");
btnAngka6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAngka6ActionPerformed(evt);
}
});
getContentPane().add(btnAngka6, new org.netbeans.lib.awtextra.AbsoluteConstraints(190, 170, 70, 30));
btnAngka7.setText("7");
btnAngka7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAngka7ActionPerformed(evt);
}
});
getContentPane().add(btnAngka7, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 210, 70, 30));
btnAngka8.setText("8");
btnAngka8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAngka8ActionPerformed(evt);
}
});
getContentPane().add(btnAngka8, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 210, 70, 30));
btnAngka9.setText("9");
btnAngka9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAngka9ActionPerformed(evt);
}
});
getContentPane().add(btnAngka9, new org.netbeans.lib.awtextra.AbsoluteConstraints(190, 210, 70, 30));
btnAngka0.setText("0");
btnAngka0.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAngka0ActionPerformed(evt);
}
});
getContentPane().add(btnAngka0, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 250, 150, 30));
btnTambah.setText("+");
btnTambah.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnTambahActionPerformed(evt);
}
});
getContentPane().add(btnTambah, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 130, 90, 30));
btnKurang.setText("-");
btnKurang.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnKurangActionPerformed(evt);
}
});
getContentPane().add(btnKurang, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 170, 90, 30));
btnKali.setText("x");
btnKali.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnKaliActionPerformed(evt);
}
});
getContentPane().add(btnKali, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 210, 90, 30));
btnBagi.setText("/");
btnBagi.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnBagiActionPerformed(evt);
}
});
getContentPane().add(btnBagi, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 250, 90, 30));
btnKoma.setText(".");
btnKoma.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnKomaActionPerformed(evt);
}
});
getContentPane().add(btnKoma, new org.netbeans.lib.awtextra.AbsoluteConstraints(190, 250, 70, 30));
judul.setFont(new java.awt.Font("Comic Sans MS", 0, 14)); // NOI18N
judul.setText("PROGRAM KALKULATOR");
getContentPane().add(judul, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 10, -1, 30));
nama.setFont(new java.awt.Font("Comic Sans MS", 0, 12)); // NOI18N
nama.setText("Desianny Amalia F. (5302413085)");
getContentPane().add(nama, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 350, -1, 30));
btnSin.setText("Sin");
btnSin.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSinActionPerformed(evt);
}
});
getContentPane().add(btnSin, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 300, 100, 30));
btnCos.setText("Cos");
btnCos.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnCosActionPerformed(evt);
}
});
getContentPane().add(btnCos, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 300, 100, 30));
btnTan.setText("Tan");
btnTan.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnTanActionPerformed(evt);
}
});
getContentPane().add(btnTan, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 300, 100, 30));
pack();
}// </editor-fold>
private void displayActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void btnSamadenganActionPerformed(java.awt.event.ActionEvent evt) {
switch(pilih)
{
case 1: //pertambahan
angka2 = Double.parseDouble(angka);
jumlah = angka1 + angka2;
angka = Double.toString(jumlah);
display.setText(angka);
break;
case 2: //pengurangan
angka2 = Double.parseDouble(angka);
jumlah = angka1 - angka2;
angka = Double.toString(jumlah);
display.setText(angka);
break;
case 3: //perkalian
angka2 = Double.parseDouble(angka);
jumlah = angka1 * angka2;
angka = Double.toString(jumlah);
display.setText(angka);
break;
case 4: //pembagian
angka2 = Double.parseDouble(angka);
jumlah = angka1 / angka2;
angka = Double.toString(jumlah);
display.setText(angka);
break;
case 5://sin
jumlah = Math.sin(Math.toRadians(angka1));
angka = Double.toString(jumlah);
display.setText(angka);
break;
case 6://cos
jumlah = Math.cos(Math.toRadians(angka1));
angka = Double.toString(jumlah);
display.setText(angka);
break;
case 7://tan
jumlah = Math.tan(Math.toRadians(angka1));
angka = Double.toString(jumlah);
display.setText(angka);
break;
default:
break;
}
}
private void btnAngka2ActionPerformed(java.awt.event.ActionEvent evt) {
angka += "2";
display.setText(angka);
}
private void btnAngka3ActionPerformed(java.awt.event.ActionEvent evt) {
angka += "3";
display.setText(angka);
}
private void btnAngka1ActionPerformed(java.awt.event.ActionEvent evt) {
angka += "1";
display.setText(angka);
}
private void btnAngka5ActionPerformed(java.awt.event.ActionEvent evt) {
angka += "5";
display.setText(angka);
}
private void btnAngka4ActionPerformed(java.awt.event.ActionEvent evt) {
angka += "4";
display.setText(angka);
}
private void btnAngka0ActionPerformed(java.awt.event.ActionEvent evt) {
angka += "0";
display.setText(angka);
}
private void btnTambahActionPerformed(java.awt.event.ActionEvent evt) {
angka1=Double.parseDouble(angka);
display.setText("+");
angka="";
pilih=1;
}
private void btnKurangActionPerformed(java.awt.event.ActionEvent evt) {
angka1=Double.parseDouble(angka);
display.setText("-");
angka="";
pilih=2;
}
private void btnAngka6ActionPerformed(java.awt.event.ActionEvent evt) {
angka += "6";
display.setText(angka);
}
private void btnAngka7ActionPerformed(java.awt.event.ActionEvent evt) {
angka += "7";
display.setText(angka);
}
private void btnAngka8ActionPerformed(java.awt.event.ActionEvent evt) {
angka += "8";
display.setText(angka);
}
private void btnAngka9ActionPerformed(java.awt.event.ActionEvent evt) {
angka += "9";
display.setText(angka);
}
private void btnKomaActionPerformed(java.awt.event.ActionEvent evt) {
angka += ".";
display.setText(angka);
}
private void btnHapusActionPerformed(java.awt.event.ActionEvent evt) {
display.setText("");
angka1=0.0;
angka2=0.0;
jumlah=0.0;
angka="";
}
private void btnKaliActionPerformed(java.awt.event.ActionEvent evt) {
angka1=Double.parseDouble(angka);
display.setText("*");
angka="";
pilih=3;
}
private void btnBagiActionPerformed(java.awt.event.ActionEvent evt) {
angka1=Double.parseDouble(angka);
display.setText("/");
angka="";
pilih=4;
}
private void btnSinActionPerformed(java.awt.event.ActionEvent evt) {
angka1=Double.parseDouble(angka);
display.setText ("SIN");
angka= "";
pilih=5;
}
private void btnCosActionPerformed(java.awt.event.ActionEvent evt) {
angka1=Double.parseDouble(angka);
display.setText ("COS");
angka= "";
pilih=6;
}
private void btnTanActionPerformed(java.awt.event.ActionEvent evt) {
angka1=Double.parseDouble(angka);
display.setText ("TAN");
angka= "";
pilih=7;
}
/**
* @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(TampilanKalkulator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(TampilanKalkulator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(TampilanKalkulator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(TampilanKalkulator.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 TampilanKalkulator().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnAngka0;
private javax.swing.JButton btnAngka1;
private javax.swing.JButton btnAngka2;
private javax.swing.JButton btnAngka3;
private javax.swing.JButton btnAngka4;
private javax.swing.JButton btnAngka5;
private javax.swing.JButton btnAngka6;
private javax.swing.JButton btnAngka7;
private javax.swing.JButton btnAngka8;
private javax.swing.JButton btnAngka9;
private javax.swing.JButton btnBagi;
private javax.swing.JButton btnCos;
private javax.swing.JButton btnHapus;
private javax.swing.JButton btnKali;
private javax.swing.JButton btnKoma;
private javax.swing.JButton btnKurang;
private javax.swing.JButton btnSamadengan;
private javax.swing.JButton btnSin;
private javax.swing.JButton btnTambah;
private javax.swing.JButton btnTan;
private javax.swing.JTextField display;
private javax.swing.JLabel judul;
private javax.swing.JLabel nama;
// End of variables declaration
}
Tidak ada komentar:
Posting Komentar