import java.util.Scanner;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GUI4 extends JFrame implements ActionListener
{ // class GUI4


public int firstNumber=0,secondNumber=0;
public char  c,operation;;
public boolean result=false;


       JTextField T1 = new  JTextField (15);
       JButton  B1 = new JButton ("1");
       JButton  B2 = new JButton ("2");
       JButton  B3 = new JButton ("3");
       JButton  B4 = new JButton ("+");
       JButton  B5 = new JButton ("4");
       JButton  B6 = new JButton ("5");
       JButton  B7 = new JButton ("6");
       JButton  B8 = new JButton ("-");
       JButton  B9 = new JButton ("7");
       JButton  B10 = new JButton ("8");
       JButton  B11 = new JButton ("9");
       JButton  B12 = new JButton ("*");
       JButton  B13 = new JButton ("0");
       JButton  B14 = new JButton ("/");
       JButton  B15 = new JButton ("=");
       JButton  B16 = new JButton ();
       JPanel p1=new JPanel();
       JPanel p2=new JPanel();


       public GUI4()
       { // constractor GUI2


           super("calculater");

           setLayout(new GridLayout(2,1));
           //setLayout(new FlowLayout());
		   T1.setBackground(Color.orange);


           p1.setLayout(new GridLayout(4,3));
           p2.setLayout(new GridLayout(4,3));
           p2.add(T1);

           p1.add(B1);
           p1.add(B2);
           p1.add(B3);
           p1.add(B4);
           p1.add(B5);
           p1.add(B6);
           p1.add(B7);
           p1.add(B8);
           p1.add(B9);
           p1.add(B10);
           p1.add(B11);
           p1.add(B12);
           p1.add(B13);
           p1.add(B16);
           p1.add(B15);
           p1.add(B14);




           B1.addActionListener(this);
           B2.addActionListener(this);
           B3.addActionListener(this);
           B4.addActionListener(this);
           B5.addActionListener(this);
           B6.addActionListener(this);
           B7.addActionListener(this);
           B8.addActionListener(this);
           B9.addActionListener(this);
           B10.addActionListener(this);
           B11.addActionListener(this);
           B12.addActionListener(this);
           B13.addActionListener(this);
           B14.addActionListener(this);
           B15.addActionListener(this);
           T1.addActionListener(this);


           add(p2);
           add(p1);

       } // constractour GUI4



      public void actionPerformed(ActionEvent e)
      { // actionPerformed


           try
           { // try
		       String s=e.getActionCommand();
		       c=s.charAt(0);

		       if(Character.isDigit(c))
		       { // if1

		           if(result)
		           { // if
		               T1.setText(""+c);
		               result=false;
		           } // end if

		           else
		               T1.setText(T1.getText()+c);
		       } // end if1

		       else

		       if(c=='.')
		       { // if2
		           T1.setText(T1.getText()+c);
		       } // end if2

		       else

		       if(c=='=')
		       { // if 3
		           secondNumber=Integer.parseInt(T1.getText());
		           switch(operation)
		           { // switch

		              case '+' : firstNumber+=secondNumber;
		              break;

		              case '-' : firstNumber-=secondNumber;
		              break;

		              case '*' : firstNumber*=secondNumber;
		              break;

		              case '/' :firstNumber/=secondNumber;
		              break;

		           } // end switch

		           result=true;
		           T1.setText(String.valueOf(firstNumber));

		       } // end if3


		       else
		       { // else
		           operation = c;
		           firstNumber = Integer.parseInt(T1.getText());
		           T1.setText("");
		       } // end else

		   } // end try

		   catch(Exception excep)
		   { // catch
		       JOptionPane.showMessageDialog(null,"Not Number","Wrong",JOptionPane.ERROR_MESSAGE);
           } // end catch


	  } // end actionPerformed



       public static void main(String args[])
       { // main
          GUI4 G=new GUI4();
          G.setBounds(200,200,250,250);
          G.setVisible(true);
          G.setResizable(false);
       }// main


   } // end class GUI4
