import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Calc extends JFrame implements ActionListener {
static final int nop = -1;
static final int add = 0;
static final int sub = 1;
static final int mul = 2;
int op = nop;
String str = new String ("");
Frame f = new BaseFrame ();
Button but1 = new Button ("1");
Button but2 = new Button ("2");
Button but3 = new Button ("3");
Button but4 = new Button ("4");
Button but5 = new Button ("5");
Button but6 = new Button ("6");
Button but7 = new Button ("7");
Button but8 = new Button ("8");
Button but9 = new Button ("9");
Button but0 = new Button ("0");
Button butadd = new Button ("+");
Button butsub = new Button ("-");
Button butmul = new Button ("*");
Button buteq = new Button ("=");
TextField tf = new TextField (str, 10);
int ax = 0;
int bx = 0;
public static class BaseFrame extends Frame {
public BaseFrame () {
setSize (300, 400);
setLocationRelativeTo (null);
setVisible (true);
}
}
public Calc () {
f.setLayout(null);
but1.addActionListener(this);
but2.addActionListener(this);
but3.addActionListener(this);
but4.addActionListener(this);
but5.addActionListener(this);
but6.addActionListener(this);
but7.addActionListener(this);
but8.addActionListener(this);
but9.addActionListener(this);
but0.addActionListener(this);
butadd.addActionListener(this);
butsub.addActionListener(this);
butmul.addActionListener(this);
buteq.addActionListener(this);
but1.setSize(50, 50);
but2.setSize(50, 50);
but3.setSize(50, 50);
but4.setSize(50, 50);
but5.setSize(50, 50);
but6.setSize(50, 50);
but7.setSize(50, 50);
but8.setSize(50, 50);
but9.setSize(50, 50);
but0.setSize(50, 50);
butadd.setSize(50, 50);
butsub.setSize(50, 50);
butmul.setSize(50, 50);
buteq.setSize(50, 50);
but1.setLocation (50, 50+50);
but2.setLocation (50+50+10, 50+50);
but3.setLocation (50+50+10+50+10, 50+50);
but4.setLocation (50, 50+50+50+10);
but5.setLocation (50+50+10, 50+50+50+10);
but6.setLocation (50+50+10+50+10, 50+50+50+10);
but7.setLocation (50, 50+50+50+10+50+10);
but8.setLocation (50+50+10, 50+50+50+10+50+10);
but9.setLocation (50+50+10+50+10, 50+50+50+10+50+10);
but0.setLocation (50+50+10, 50+50+50+10+50+10+50+10);
butadd.setLocation (25, 50+50+50+10+50+10+50+10+50+10);
butsub.setLocation (25+50+10, 50+50+50+10+50+10+50+10+50+10);
butmul.setLocation (25+50+10+50+10, 50+50+50+10+50+10+50+10+50+10);
buteq.setLocation (25+50+10+50+10+50+10, 50+50+50+10+50+10+50+10+50+10);
f.add (but1);
f.add (but2);
f.add (but3);
f.add (but4);
f.add (but5);
f.add (but6);
f.add (but7);
f.add (but8);
f.add (but9);
f.add (but0);
f.add (butadd);
f.add (butsub);
f.add (butmul);
f.add (buteq);
tf.setLocation (25, 50);
tf.setSize (255, 25);
tf.setEditable (false);
f.add (tf);
}
public static void main (String [] args) {
Calc calc = new Calc();
}
public void actionPerformed (ActionEvent ae){
if(ae.getSource() == this.but1){
this.str = this.str + "1";
tf.setText(this.str);
}
else if(ae.getSource() == this.but2){
this.str = this.str + "2";
tf.setText(this.str);
}
else if (ae.getSource() == this.but3){
this.str = this.str + "3";
tf.setText(this.str);
}
else if (ae.getSource() == this.but4){
this.str = this.str + "4";
tf.setText(this.str);
}
else if (ae.getSource() == this.but5){
this.str = this.str + "5";
tf.setText(this.str);
}
else if (ae.getSource() == this.but6){
this.str = this.str + "6";
tf.setText(this.str);
}
else if (ae.getSource() == this.but7){
this.str = this.str + "7";
tf.setText(this.str);
}
else if (ae.getSource() == this.but8){
this.str = this.str + "8";
tf.setText(this.str);
}
else if (ae.getSource() == this.but9){
this.str = this.str + "9";
tf.setText(this.str);
}
else if (ae.getSource() == this.but0){
this.str = this.str + "0";
tf.setText(this.str);
}
else if (ae.getSource() == this.butadd){
if (op == nop) {
op = add;
ax = Integer.parseInt(this.str);
this.str = "";
tf.setText(this.str);
}
else {
tf.setText ("Please don't perform more the one operation per time");
}
}
else if (ae.getSource() == this.butmul){
if (op == nop) {
op = mul;
ax = Integer.parseInt(this.str);
this.str = "";
tf.setText(this.str);
}
else {
tf.setText ("Please don't perform more the one operation per time");
}
}
else if (ae.getSource() == this.butsub){
if (op == nop) {
op = sub;
ax = Integer.parseInt(this.str);
this.str = "";
tf.setText(this.str);
}
else {
tf.setText ("Please don't perform more the one operation per time");
}
}
else if (ae.getSource() == this.buteq){
if (op == nop) {
tf.setText ("Please give operation and second operand first");
}
else if (op == add) {
op = nop;
ax = ax + Integer.parseInt(this.str);
this.str = Integer.toString (ax);
tf.setText(this.str);
}
else if (op == mul) {
op = nop;
ax = ax * Integer.parseInt(this.str);
this.str = Integer.toString (ax);
tf.setText(this.str);
}
else if (op == sub) {
op = nop;
ax = ax - Integer.parseInt(this.str);
this.str = Integer.toString (ax);
tf.setText(this.str);
}
}
}
}