import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class Kalkulator01 implements ActionListener {
JFrame j = new JFrame("Kalkulator");
Container c = j.getContentPane();
JButton b1 = new JButton("+");
JButton b2 = new JButton("-");
JButton b3 = new JButton("*");
JButton b4 = new JButton("/");
JTextField t1=new JTextField(20);
JTextField t2=new JTextField(20);
JTextField t3=new JTextField(20);
public Kalkulator01() {
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j.setSize(250, 200);
j.setVisible(true);
c.setBackground(Color.PINK);
c.setLayout(new FlowLayout());
c.add(t1);
c.add(t2);
c.add(t3);
c.add(b1);
c.add(b2);
c.add(b3);
c.add(b4);
t1.addActionListener(this);
t2.addActionListener(this);
t3.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
}
public static void main(String[] args) {
new Kalkulator01();
}
@Override
public void actionPerformed(ActionEvent e) {
float x=0;
float y=0;
float hasil=0;
try{
x=Float.parseFloat(t1.getText());
y=Float.parseFloat(t2.getText());
}catch(Exception ex){
ex.printStackTrace();
JOptionPane.showMessageDialog(j, "Harus Bilangan! ");
}
if(e.getSource()==b1){
hasil=x+y;
}else if(e.getSource()==b2){
hasil=x-y;
}else if(e.getSource()==b3){
hasil=x*y;
}else{
hasil=x/y;
}
t3.setText(new String().valueOf(hasil));
System.out.println("Hasil :"+t3.getText());
}
}
Tidak ada komentar:
Posting Komentar