Selasa, 20 Juli 2010

Program Kalkulator GUI Culun Menggunakan Java AWT

Berikut kode sumber program kalkulator berbasis grafis menggunakan Java AWT. Kenapa culun? karena memang culun... ngga percaya? coba aja sendiri!

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

public class Kalkulator extends Frame implements WindowListener, ActionListener {
TextField txtBil1, txtOpr, txtBil2, txtHasil;
Button btnHitung;

/** Creates a new instance of Kalkulator */
public Kalkulator(String judul) {
super(judul);
this.addWindowListener(this);
this.setLayout(new FlowLayout(FlowLayout.LEFT,10,10));
txtBil1 = new TextField();
txtBil1.setColumns(5);
txtOpr = new TextField();
txtOpr.setColumns(5);
txtBil2 = new TextField();
txtBil2.setColumns(5);
txtHasil = new TextField();
txtHasil.setColumns(5);
btnHitung = new Button(" = ");
btnHitung.addActionListener(this);
add(txtBil1);
add(txtOpr);
add(txtBil2);
add(btnHitung);
add(txtHasil);
}
public void windowActivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowClosing(WindowEvent e) {
setVisible(false);
System.exit(0);
}
public void windowDeactivated(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}

public void actionPerformed(ActionEvent e) {
double a, b, hasil;
String c;
if (e.getSource() == btnHitung){
a = Double.parseDouble(txtBil1.getText());
b = Double.parseDouble(txtBil2.getText());
c = txtOpr.getText();
if (c.equals("*")) {
hasil = a * b;
} else if (c.equals("/")) {
hasil = a / b;
} else if (c.equals("+")) {
hasil = a + b;
} else if (c.equals("-")) {
hasil = a - b;
} else {
hasil = 0;
}

txtHasil.setText(""+hasil);
}
}

public static void main(String args[]) {
Kalkulator frm = new Kalkulator("Kalkulator Culun");
frm.setSize(500, 200);
frm.setVisible(true);
}
}

Tidak ada komentar: