- Terlebih dahulu buat desain tampilan kalkulator dengan java swing.
- Beri nama variabel pada tiap komponen tombol fungsi ( ex. tombol tambah (+) beri nama variabel "tambah")
- Klik kanan pada tombol --> Events --> Action --> ActionPerformed[tambahactionPerformed]
- Masuk ke jendela Source, di bawah // TODO....., buat kodingannya seperti contoh kodingan ini.
- Cara yang sama dilakukan untuk tiap-tiap komponen.
public class kalkulator_Sederhana extends javax.swing.JFrame {
private String str = ("");
private double a,b;
/** Creates new form kalkulator Sederhana */
public kalkulator_Sederhana (){
initComponents ();
}
private void tambahActionPerformed (java.awt.event.ActionEvent evt){
// TODO add your handling code here:
a=Double.parseDouble(t1.getText());
b=Double.parseDouble(t2.getText());
double hasil = a+b;
angkahasil.setText(str+hasil);
}
private void kurangActionPerformed (java.awt.event.ActionEvent evt){
// TODO add your handling code here:
a=Double.parseDouble(t1.getText());
b=Double.parseDouble(t2.getText());
double hasil = a-b;
angkahasil.setText(str+hasil);
}
private void kaliActionPerformed (java.awt.event.ActionEvent evt){
// TODO add your handling code here:
a=Double.parseDouble(t1.getText());
b=Double.parseDouble(t2.getText());
double hasil = a*b;
angkahasil.setText(str+hasil);
}
private void bagiActionPerformed (java.awt.event.ActionEvent evt){
// TODO add your handling code here:
a=Double.parseDouble(t1.getText());
b=Double.parseDouble(t2.getText());
double hasil = a/b;
angkahasil.setText(str+hasil);
}
private void hapusActionPerformed (java.awt.event.ActionEvent evt){
// TODO add your handling code here:
t1.setText("");
t2.setText("");
angkahasil.setText("");
}
public static void main (String args[]) {
/* set the Nimbus look and feel */
/*Create and display the form */
java.awt.EventQueue.invokeLater (new Runnable() {
public void run() {
new kalkulator_Sederhana().setVisible(true);
}
});
}
Selamat mencoba ..............
Tidak ada komentar:
Posting Komentar