import java.awt.*;
import java.awt.event.*;
class AplikasiKalkulator extends JFrame implements ActionListener
{
private Container container = new Container();
JPanel panel1= new JPanel();
JPanel panel2= new JPanel();
JPanel panel3= new JPanel();
private JLabel label1 = new JLabel("Input A = ");
private JLabel label2 = new JLabel("Input B = ");
private TextField txt1=new TextField(20);
private TextField txt2=new TextField(20);
private JButton cmd1= new JButton ("-");
private JButton cmd2= new JButton ("+");
private JButton cmd3= new JButton ("/");
private JButton cmd4= new JButton ("*");
private JButton cmd5=new JButton("Exit");
private JTextArea txtarea1=new JTextArea(7,20);
String Hasil="";
AplikasiKalkulator() { setTitle("Kalkulator");
setLocation(300,100); setSize(300,210);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel1.setLayout(new GridLayout(2,2));
panel1.add(label1); panel1.add(txt1);
panel1.add(label2); panel1.add(txt2);
panel2.setLayout(new GridLayout(5,1));
panel2.add(cmd1); panel2.add(cmd2);
panel2.add(cmd3); panel2.add(cmd4);
panel2.add(cmd5); panel3.add(txtarea1);
container = getContentPane();
container.setLayout(new BorderLayout());
container.add (panel1, BorderLayout.NORTH);
container.add(panel2,BorderLayout.EAST);
container.add(panel3,BorderLayout.WEST);
cmd1.addActionListener(this);
cmd2.addActionListener(this);
cmd3.addActionListener(this);
cmd4.addActionListener(this);
cmd5.addActionListener(this);
setVisible(true);
}
public void actionPerformed (ActionEvent e)
{
Object objek = e.getSource();
if (objek == cmd1)
{
double x= Double.parseDouble(txt1.getText());
double y= Double.parseDouble(txt2.getText());
String z= String.valueOf(x-y);
txtarea1.append(txt1.getText()+" dikurangi "+txt2.getText()+" = "+z+"\n");
}
else if
(objek == cmd2)
{
double x= Double.parseDouble(txt1.getText());
double y= Double.parseDouble(txt2.getText());
String z= String.valueOf(x+y);
txtarea1.append(txt1.getText()+" ditambah "+txt2.getText()+" = "+z+"\n");
}
else if
(objek == cmd3)
{
double x= Double.parseDouble(txt1.getText());
double y= Double.parseDouble(txt2.getText());
String z= String.valueOf(x/y);
txtarea1.append(txt1.getText()+" dibagi "+txt2.getText()+" = "+z+"\n");
}
else if
(objek == cmd4)
{
double x= Double.parseDouble(txt1.getText());
double y= Double.parseDouble(txt2.getText());
String z= String.valueOf(x*y);
txtarea1.append(txt1.getText()+" dikali "+txt2.getText()+" = "+z+"\n");
}
else if
(objek == cmd5)
{
System.exit(0);
}
}
public static void main(String args[]) { new AplikasiKalkulator();
}
}
Masukkan Input 1
Masukkan Input 2
Hasilnya Seperti Di bawah iniSumber: http://dewa-pemrograman.blogspot.com/2013/08/contoh-source-code-program-kalkulator.html
Tidak ada komentar:
Posting Komentar