Sunday, November 4, 2012

Simple calculator using Java Applet and Event Handling

You can see all event methods in java.awt.Event class
and java.awt.event.ActionListener is an Interface that includes method named actionPerformed and takes ActionEvent as an argument.
Type javap java.awt.event.ActionListener in Command Prompt.


Step 1:Create a java source file with name calc.java that is name of java class and it is mandatory that class-name and file-name must be same.

Applet class must be public.

In java source file commented applet code will not be compile by java compiler but will executed by AppletViewer.

In command addActionListener(this) "this" keyword refers as object of current class. we can also write "new calc()" at place of "this" keyword.For making any TextField ReadOnly we have to use method TextField().setEnabled(false)
 import java.awt.*;  
 import java.applet.*;  
 import java.awt.event.*;  
 //<applet code="calc.class" width=150 height=300></applet>  
 public class calc extends Applet implements ActionListener  
 {  
 Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,ba,bs,br,bc;  
 TextField t1,t2,t3;  
 Label l1,l2,l3;  
 String m="";  
 int i,a;  
      public void init()  
      {  
      l1=new Label("Enter No.");  
      add(l1);  
      t1=new TextField(15);  
      add(t1);  
      b1=new Button("1");  
      b2=new Button("2");  
      b3=new Button("3");  
      b4=new Button("4");  
      b5=new Button("5");  
      b6=new Button("6");  
      b7=new Button("7");  
      b8=new Button("8");  
      b9=new Button("9");  
      b0=new Button("0");  
      ba=new Button("Add");  
      bs=new Button("Sub");  
      br=new Button("Result");  
      bc=new Button("Clear");  
      add(b1);  
      add(b2);  
      add(b3);  
      add(b4);  
      add(b5);  
      add(b6);  
      add(b7);  
      add(b8);  
      add(b9);  
      add(b0);  
      add(ba);  
      add(bs);  
      add(br);  
      add(bc);  
      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);  
      b0.addActionListener(this);  
      ba.addActionListener(this);  
      bs.addActionListener(this);  
      br.addActionListener(this);  
      bc.addActionListener(this);  
      l2=new Label("Result");  
      add(l2);  
      t2=new TextField(15);  
      add(t2);  
      t2.setEnabled(false);  
      l3=new Label("Exception");  
      add(l3);  
      t3=new TextField(40);  
      add(t3);  
      t3.setEnabled(false);  
      }  
      public void actionPerformed(ActionEvent e)  
      {  
      try{  
      if(e.getActionCommand().equals("1"))  
           {     m+="1";  
                t1.setText(m);}  
      if(e.getActionCommand().equals("2"))  
           {     m+="2";  
                t1.setText(m);}  
      if(e.getActionCommand().equals("3"))  
           {     m+="3";  
                t1.setText(m);}  
      if(e.getActionCommand().equals("4"))  
           {     m+="4";  
                t1.setText(m);}  
      if(e.getActionCommand().equals("5"))  
           {     m+="5";  
                t1.setText(m);}  
      if(e.getActionCommand().equals("6"))  
           {     m+="6";  
                t1.setText(m);}  
      if(e.getActionCommand().equals("7"))  
           {     m+="7";  
                t1.setText(m);}  
      if(e.getActionCommand().equals("8"))  
           {     m+="8";  
                t1.setText(m);}  
      if(e.getActionCommand().equals("9"))  
           {     m+="9";  
                t1.setText(m);}  
      if(e.getActionCommand().equals("0"))  
           {     m+="0";  
                t1.setText(m);}  
      if(e.getActionCommand().equals("Add"))  
           {i=Integer.parseInt(t1.getText());       
                t1.setText("");  
                m="";  
                a=1;}  
      if(e.getActionCommand().equals("Sub"))  
           {i=Integer.parseInt(t1.getText());       
                t1.setText("");  
                m="";  
                a=2;}  
      if(e.getActionCommand().equals("Result"))  
           {     if(a==1)  
                {  
                int j=Integer.parseInt(t1.getText());  
                int k=i+j;  
                Integer z=new Integer(k);  
                String s=z.toString();  
                t2.setText(s);       
                t1.setText("");  
                m="";  
                }  
                else if(a==2)  
                {                 
                int j=Integer.parseInt(t1.getText());  
                int k=i-j;  
                Integer z=new Integer(k);  
                String s=z.toString();  
                t2.setText(s);       
                t1.setText("");  
                m="";  
                }  
                else  
                {}  
           }  
      if(e.getActionCommand().equals("Clear"))  
           {       
                t1.setText("");  
                t2.setText("");  
                t3.setText("");}  
      }  
      catch(Exception e1){  
      String s=e1.toString();  
      t3.setText(s);}  
      }  
 }                  

Step 2: Run cmd(command prompt) and compile java source file, calc.class file'll be build.

then give appletviewer command and applet will pop out.


output:


Download Code Link 1 (3 kb)
Download Code Link 2 (3 kb)













Also Read Simple calculator using Windows form Application with C# in VS2008

No comments:

Popular Posts