GUI Kalkulator nihhhh…

Lagi-lagi saya memberi referensi tentang GUI.Tapi untuk kali ini akan ditampilkan bagaimana membuat kalkulator GUI..sederhana memang tapi ga ada salahnya dipelajari bersama biar skill java kita makin mengerikan..hahaha
Monggo diliat yuuuk..

Listing:

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

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

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);
}
}

About halojack

i'm a simple guy

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: