Kali ini saya membuat suatu program " KALKULATOR SEDERHANA "
Terdiri dari + ,- ,x ,:
SILAHKAN DICOBA !!!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class kalkulator extends JFrame implements ActionListener
{
private JLabel judul = new JLabel("Kalkulator buatan 1IA03");
private JLabel nil1 = new JLabel("Masukan Nilai A"),
nil2 = new JLabel("Masukan Nilai B"),
has = new JLabel("Hasil");
private JTextField nilai1 = new JTextField(),
nilai2 = new JTextField(),
hasil = new JTextField();
private JButton tambah = new JButton("+");
private JButton kurang = new JButton("-");
private JButton bagi = new JButton("/");
private JButton kali = new JButton("X");
private JButton hapus = new JButton("Hapus");
kalkulator() {
setTitle("Kalkulator sederhana");
setSize(500,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
getContentPane().add(judul);
judul.setBounds(120,10,150,20);
getContentPane().setLayout(null);
getContentPane().add(nil1);
nil1.setBounds(10,60,100,20);
getContentPane().add(nil2);
nil2.setBounds(10,100,100,20);
getContentPane().add(has);
has.setBounds(10,140,100,20);
getContentPane().add(nilai1);
nilai1.setBounds(200,60,100,20);
getContentPane().add(nilai2);
nilai2.setBounds(200,100,100,20);
getContentPane().add(hasil);
hasil.setBounds(200,140,100,20);
hasil.setEditable(false);
getContentPane().add(tambah);
tambah.setBounds(10,200,100,20);
getContentPane().add(kurang);
kurang.setBounds(200,200,100,20);
getContentPane().add(bagi);
bagi.setBounds(10,250,100,20);
getContentPane().add(kali);
kali.setBounds(200,250,100,20);
getContentPane().add(hapus);
hapus.setBounds(200,300,100,20);
tambah.addActionListener(this);
kurang.addActionListener(this);
bagi.addActionListener(this);
kali.addActionListener(this);
hapus.addActionListener(this);
}
public void actionPerformed(ActionEvent x){
double a=0.0,b=0.0,c=0.0;
try{
a=Double.parseDouble(nilai1.getText());
b=Double.parseDouble(nilai2.getText());
}
catch (Exception e){
JOptionPane.showMessageDialog(null ,"Angka yang anda masukan salah","Kesalahan",JOptionPane.ERROR_MESSAGE);
}if (x.getSource()==bagi)
{
c=a/b;
}else
if (x.getSource()==hapus)
{
c=0;
}else
if (x.getSource()==kali)
{
c=a*b;
}else
if (x.getSource()==tambah)
{
c=a+b;
}else
if (x.getSource()==kurang)
{
c=a-b;
}
hasil.setText(new String().valueOf(c));
}
public static void main(String[] args){
new kalkulator();
}
}
Tidak ada komentar:
Posting Komentar