Rabu, 01 Desember 2010

Source Code Program Kalkulator Java GUI AWT nan Katrok


Hehehe... berikut ini saya terbitkan kode sumber alias source code program kalkukatrok ups! kalkulator GUI menggunakan AWT. Kenapa AWT lagi? yaaahhhhh suka-suka gue laahhhh. Yang perlu digarisbawahi : ini program bener-bener katrok... kalo masih ada bug-nya... yaaa harap dimaafkeun... namanya juga katrok! hehehe

Kali ini program menggunakan WindowListener dalam bentuk anonymous inner class untuk menyingkat penulisan source code (bandingkan dengan program kalkulator GUI Java AWT culun [lihat] yang masih meng-implements WindowListener)

import java.awt.*;
import java.awt.event.*;

public class Kalkukatrok extends Frame implements ActionListener {
Button btn0,btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9;
Button btnAdd,btnSub,btnMul,btnDiv,btnEqu,btnAC;
double bil1=0, bil2=0, hasil=0;
String opr="";
Label lblTampil;
boolean hapus = false;

public Kalkukatrok(String title) {
super(title);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
dispose();
System.exit(1);
}
});
this.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 10));
btn0 = new Button("0");
btn1 = new Button("1");
btn2 = new Button("2");
btn3 = new Button("3");
btn4 = new Button("4");
btn5 = new Button("5");
btn6 = new Button("6");
btn7 = new Button("7");
btn8 = new Button("8");
btn9 = new Button("9");
btnAdd = new Button("+");
btnSub = new Button("-");
btnMul = new Button("x");
btnDiv = new Button(":");
btnEqu = new Button("=");
btnAC = new Button("AC");
lblTampil = new Label(){
public Dimension getPreferredSize() {
return getSize();
}
};
lblTampil.setSize(200,20);
btn0.addActionListener(this);
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
btn5.addActionListener(this);
btn6.addActionListener(this);
btn7.addActionListener(this);
btn8.addActionListener(this);
btn9.addActionListener(this);
btnAdd.addActionListener(this);
btnSub.addActionListener(this);
btnMul.addActionListener(this);
btnDiv.addActionListener(this);
btnEqu.addActionListener(this);
btnAC.addActionListener(this);
add(lblTampil);
add(btn0);
add(btn1);
add(btn2);
add(btn3);
add(btn4);
add(btn5);
add(btn6);
add(btn7);
add(btn8);
add(btn9);
add(btnAdd);
add(btnSub);
add(btnMul);
add(btnDiv);
add(btnEqu);
add(btnAC);
setSize(210,180);
setVisible(true);
}

public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn0){
hapus();
tampil("0");
} else if (e.getSource() == btn1){
hapus();
tampil("1");
} else if (e.getSource() == btn2){
hapus();
tampil("2");
} else if (e.getSource() == btn3){
hapus();
tampil("3");
} else if (e.getSource() == btn4){
hapus();
tampil("4");
} else if (e.getSource() == btn5){
hapus();
tampil("5");
} else if (e.getSource() == btn6){
hapus();
tampil("6");
} else if (e.getSource() == btn7){
hapus();
tampil("7");
} else if (e.getSource() == btn8){
hapus();
tampil("8");
} else if (e.getSource() == btn9){
hapus();
tampil("9");
} else if (e.getSource() == btnAdd) {
opr = "+";
bil1=Double.parseDouble(lblTampil.getText());
lblTampil.setText("");
} else if (e.getSource() == btnSub) {
opr = "-";
bil1=Double.parseDouble(lblTampil.getText());
lblTampil.setText("");
} else if (e.getSource() == btnMul) {
opr = "x";
bil1=Double.parseDouble(lblTampil.getText());
lblTampil.setText("");
} else if (e.getSource() == btnDiv) {
opr = ":";
bil1=Double.parseDouble(lblTampil.getText());
lblTampil.setText("");
} else if (e.getSource() == btnEqu) {
bil2 = Double.parseDouble(lblTampil.getText());
hapus = true;
if (opr.equals("+")) {
hasil = bil1 + bil2;
lblTampil.setText(""+hasil);
} else if (opr.equals("-")) {
hasil = bil1 - bil2;
lblTampil.setText(""+hasil);
} else if (opr.equals("x")) {
hasil = bil1 * bil2;
lblTampil.setText(""+hasil);
} else if (opr.equals(":")) {
hasil = bil1 / bil2;
lblTampil.setText(""+hasil);
}
} else if (e.getSource() == btnAC) {
hasil = 0;
bil1 = 0;
bil2 = 0;
opr="";
hapus = false;
lblTampil.setText("");
}
}

void hapus() {
if (hapus == true) {
lblTampil.setText("");
hapus = false;
}
}

void tampil(String teks) {
lblTampil.setText(lblTampil.getText()+teks);
}

public static void main(String args[]) {
Kalkukatrok frm = new Kalkukatrok("Kalkukatrok");
}
}
Oh iya, kalo nemu bug-nya dan bisa membuang bug-nya, bagi-bagi ya! ciao!

1 komentar:

njjhv mengatakan...

ooo, iya masih ada bug nya yaa..
ayo kita perbaiki..