Rabu, 27 Juni 2012
KALKULATOR APPLET JAVA SEDERHANA
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
public class program extends Applet {
JTextField f1,f2,f3;
JLabel angka1,angka2,hasil;
Choice cPilihan;
TextArea tArea;
public void init() {
JLabel angka1 = new JLabel ("angka1");
f1 = new JTextField(" ",10);
JLabel angka2 = new JLabel ("angka2");
f2 = new JTextField(" ",10);
JLabel hasil = new JLabel ("hasil");
f3 = new JTextField(" ",10);
JPanel P1 = new JPanel();
P1.setLayout(new GridLayout(6,6));
P1.add(angka1);
P1.add(f1);
P1.add(angka2);
P1.add(f2);
P1.add(hasil);
P1.add(f3);
add("North",P1);
program oby = new program();
cPilihan = new Choice();
cPilihan.add("+");
cPilihan.add("-");
cPilihan.add("/");
cPilihan.add("*");
cPilihan.addItemListener(new program.PenanganItem());
tArea = new TextArea(" ",3,20,TextArea.SCROLLBARS_NONE);
tArea.setEditable(false);
JPanel P2 = new JPanel();
P2.add(cPilihan);
P2.add(tArea);
add("East",P2);
}
class PenanganItem implements ItemListener{
public void itemStateChanged(ItemEvent e){
tArea.setText("Pilihan:"+" "+cPilihan.getSelectedItem());
String s =cPilihan.getSelectedItem();
if(s.equals("+")){
double x,y,z;
x = Double.parseDouble(f1.getText());
y = Double.parseDouble(f2.getText());
z = x+y;
f3.setText(new Double(z).toString());
}
if(s.equals("-")){
double x,y,z;
x = Double.parseDouble(f1.getText());
y = Double.parseDouble(f2.getText());
z = x-y;
f3.setText(new Double(z).toString());
}
if(s.equals("/")){
double x,y,z;
x = Double.parseDouble(f1.getText());
y = Double.parseDouble(f2.getText());
z = x/y;
f3.setText(new Double(z).toString());
}
if(s.equals("*")){
double x,y,z;
x = Double.parseDouble(f1.getText());
y = Double.parseDouble(f2.getText());
z = x*y;
f3.setText(new Double(z).toString());
}
}
}
}
Langganan:
Posting Komentar (Atom)
Tidak ada komentar:
Posting Komentar