import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Computer extends Frame implements ActionListener{
Button bt_add = new Button("+");
Button bt_sub = new Button("-");
Button bt1 = new Button("1");
Button bt2 = new Button("2");
Button bt3 = new Button("3");
TextField txt1 = new TextField();
TextField txt2 = new TextField();
TextField txt3 = new TextField();
TextField txt4 = new TextField();
public Computer() {
this.setLayout(new FlowLayout());
this.add(txt1);
this.add(txt2);
this.add(txt3);
this.add(txt4);
this.add(bt1);
this.add(bt2);
this.add(bt3);
this.add(bt_add);
this.add(bt_sub);
bt1.addActionListener(this);
bt2.addActionListener(this);
bt3.addActionListener(this);
bt_add.addActionListener(this);
bt_sub.addActionListener(this);
this.addWindowListener(new Windows());
}
@Override
public void actionPerformed(ActionEvent e) {
double d1,d2,result;
String op;
d1 = Double.parseDouble(txt1.getText());
d2 = Double.parseDouble(txt3.getText());
op = txt2.getText();
if(op.equals("+") ) {
result = d1+d2;
txt4.setText(Double.toString(result));
}
else if(op.equals("-")) {
result = d1-d2;
txt4.setText(Double.toString(result));
}
}
public static void main(String[] args) {
Computer c = new Computer();
c.setSize(240,240);
c.setVisible(true);
}
}
class Windows extends WindowAdapter{
public void WindowClosing(WindowEvent e) {
System.exit(2);
}
}