Wednesday, 20 July 2011

Applet Example


import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class AwtClass extends Applet implements ActionListener
{
            String result="";
            TextField FirstText,SecondText;
            Button Add,Sub,Div,Mul;
            public void init()
            {
                        //Label
                        Label FirstLabel=new Label("First Number ",Label.RIGHT);
                        Label SecondLabel=new Label("Second Number ",Label.RIGHT);
                        Label AnsLabel=new Label("Result ",Label.RIGHT);                    

                        //TextField
                        FirstText=new TextField(5);
                        SecondText=new TextField(5);

                        //Button
                        Button Add=new Button("Add");
                        Button Sub=new Button("Subtract");
                        Button Div=new Button("Division");
                        Button Mul=new Button("Multiply");


                        add(FirstLabel);
                        add(FirstText);
                        add(SecondLabel);                   
                        add(SecondText);
                        add(Add);
                        add(Sub);
                        add(Div);
                        add(Mul);

                        Add.addActionListener(this);
                        Sub.addActionListener(this);
                        Div.addActionListener(this);
                        Mul.addActionListener(this);
            }
            public void actionPerformed(ActionEvent ae)
            {
                        try
                        {
                                    String op=ae.getActionCommand();
                                    if(op.equals("Add"))
                                    {                      
                                                result=(Integer.toString(Integer.parseInt(FirstText.getText())+Integer.parseInt(SecondText.getText())));                               
                                    }
                                    else if(op.equals("Subtract"))
                                    {                                                          
                                                result=(Integer.toString(Integer.parseInt(FirstText.getText())-Integer.parseInt(SecondText.getText())));                        
                                    }          
                                    else if(op.equals("Division"))
                                    {                      
                                                result=(Integer.toString(Integer.parseInt(FirstText.getText())/Integer.parseInt(SecondText.getText())));                                
                                    }
                                    else
                                    {                      
                                                result=(Integer.toString(Integer.parseInt(FirstText.getText())*Integer.parseInt(SecondText.getText())));                                
                                    }
                        }
                        catch(Exception e)
                        {
                                    result=e.getMessage();
                        }
                        repaint();
            }
            public void paint(Graphics g)
            {
                        g.drawString("Output : "+result,6,80);
            }
}

File Name : AwtClass.html
<applet code="AwtClass.class" width=350 height=100>
</applet>

No comments:

Post a Comment