import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.KeyEvent;
public class Calculator extends Frame implements ActionListener
{
 Button b[]=new Button[16];
  TextField tf;
  Panel pi1;
  Panel pi2;
 String s="";
  Font f12 = new Font("Times New Roman",Font.BOLD,25);
  Font f10 = new Font("SansSerif",Font.BOLD,10);
  Font f121 = new Font("SansSerif", Font.ITALIC, 25);
  Menu menuFile, menuHelp;
  MenuItem menuitemExit, menuitemAbout;
  //int r,n1;
  float res,r,n1;
 char c;
  public Calculator()
  {
   setBackground(Color.blue);
   menuFile = new Menu("File");
   menuFile.setFont(f10);
   ÂÂ
   menuitemExit = new MenuItem("Exit");
   menuitemExit.setFont(f10);
   menuFile.add(menuitemExit);
   menuHelp = new Menu("About");
   menuHelp.setFont(f10);
  ÂÂ
   menuitemAbout = new MenuItem("Contact");
   menuitemAbout.setFont(f10);
   menuitemAbout.addActionListener(this);
   menuHelp.add(menuitemAbout);
    ÂÂ
   MenuBar mb = new MenuBar();
   mb.add(menuFile);
   mb.add(menuHelp);
   setMenuBar(mb);
  ÂÂ
   tf=new TextField(34);
   setLayout(new BorderLayout());
 pi1=new Panel();
   pi2=new Panel();
   pi1.setBackground(Color.PINK);
   pi2.setBackground(Color.WHITE);
   pi2.setLayout(new GridLayout(4,2));
 pi1.add(tf);
 setTitle("Calculator");
   for(int i=0;i<10;i++)
 {
 b[i]=new Button(i+" ");
 b[i].addActionListener(this);
 pi2.add(b[i]);
 }
   for (int i=0; i<10; i++)
   {
    b[i].setFont(f12);
    b[i].setForeground(Color.blue);
   }
   b[10]=new Button("+");
 b[11]=new Button("-");
 b[12]=new Button("*");
 b[13]=new Button("/");
 b[14]=new Button("c");
 b[15]=new Button("=");
   for (int i=10; i<16; i++)
   {
    b[i].setFont(f121);
    b[i].setForeground(Color.red);
   }
 pi2.add(b[10]);
 pi2.add(b[11]);
 pi2.add(b[12]);
 pi2.add(b[13]);
 pi2.add(b[14]);
 pi2.add(b[15]);
 add("North",pi1);
 add("Center",pi2);
 b[10].addActionListener(this);
 b[11].addActionListener(this);
 b[12].addActionListener(this);
 b[13].addActionListener(this);
 b[14].addActionListener(this);
 b[15].addActionListener(this);
 addWindowListener(new WinExit());
 }
 public void actionPerformed(ActionEvent e )
  {
   if(e.getSource() == menuitemAbout)
   {
    Dialog dlgAbout = new CustomABOUTDialog(this, "About me", true);
    dlgAbout.setVisible(true);
   }
   for(int k=0;k<10;k++)
   {
 if(e.getSource().equals(b[k]))
    {
 tf.setText(s+k);
 s=s+k;
    }
 }
 r=Integer.parseInt(tf.getText());
 if(e.getSource().equals(b[10]))
 {
 n1=r;
 s="";c='+';
 }
 if(e.getSource().equals(b[11]))
 {
 n1=r;
 s="";c='-';
 }
 if(e.getSource().equals(b[12]))
 {
 n1=r;
 s="";c='*';
 }
 if(e.getSource().equals(b[13]))
 {
 n1=r;
 s="";c='/';
 }
   if(e.getSource().equals(b[14]))
 {
 n1=r;
 s="";
 }
 if(e.getSource().equals(b[15]))
 {
 float a=0;
 switch(c)
 {
 case '+':
 a=r;
 res=n1+a;
 break;
 case '-':
 a=r;
 res=n1-a;
 break;
 case '*':
 a=r;
 res=n1*a;
 break;
 case '/':
 a=r;
 res=n1/a;
 break;
 }
 //System.out.println(res);
 tf.setText(res+"");
 s="";
 }
 if(e.getSource().equals(b[14]))
   {
 tf.setText(0+"");
 s="";
 }
 }
 public static void main(String args[])
 {
 Calculator c1=new Calculator();
 c1.setSize(300,300);
 c1.setVisible(true);
   c1.setTitle("My Calculator");
   c1.setResizable(false);
 }
}
class WinExit extends WindowAdapter
{
 public void windowClosing(WindowEvent e)
 {
 System.exit(0);
 }
}
class CustomABOUTDialog extends Dialog
{
   CustomABOUTDialog(Frame parent, String title, boolean modal){
   super(parent, title, modal);
   setBackground(Color.black);
  ÂÂ
   Panel p1 = new Panel(new FlowLayout(FlowLayout.CENTER));
   StringBuffer text = new StringBuffer();
   text.append("(TE-IT)\n\n");
   TextArea tAreaAbout = new TextArea(10,40);
   tAreaAbout.setText(text.toString());
   tAreaAbout.setFont(new Font("Times New Roman", 1, 13));
   tAreaAbout.setEditable(false);
   p1.add(tAreaAbout);
   p1.setBackground(Color.PINK);
   add(p1, BorderLayout.CENTER);
  ÂÂ
   setLocation(408, 350);
   setResizable(false);
   addWindowListener(new WindowAdapter() {
     public void windowClosing(WindowEvent e)
     {
      Window aboutDialog = e.getWindow();
      aboutDialog.dispose();
      //System.exit(0);
     }
    }
   );
   pack();
  }
}
Copyright © 2011 - All Rights Reserved - Softron.in
Template by Softron Technology