import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class kalkulator extends JFrame {
JTextField f1,f2,f3;
JLabel angka1,angka2,hasil;
public static void main (String [] args){
kalkulator ap1 = new kalkulator();}
public kalkulator(){
super("KALKULATOR SEDERHANA");
setSize(300,300);
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);
getContentPane().add("North",P1);
JButton tombolPertambahan = new JButton("+");
JButton tombolPengurangan = new JButton("-");
JButton tombolPembagi = new JButton("/");
JButton tombolPerkalian = new JButton("*");
JPanel panelTombol = new JPanel();
panelTombol.add(tombolPertambahan);
panelTombol.add(tombolPengurangan);
panelTombol.add(tombolPembagi);
panelTombol.add(tombolPerkalian);
tombolPertambahan.addActionListener(new PenanganTombol());
tombolPengurangan.addActionListener(new PenanganTombol());
tombolPembagi.addActionListener(new PenanganTombol());
tombolPerkalian.addActionListener(new PenanganTombol());
getContentPane().add("East",panelTombol);
addWindowListener(new PenanganKejadian());
show();
}
class PenanganKejadian extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
class PenanganTombol implements ActionListener{
public void actionPerformed(ActionEvent e){
String s = e.getActionCommand();
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