Tuesday 3 September 2019

Applet calculator program in Java | Simple calculator program in Java using awt

               In this post, we will see applet calculator program in java | simple calculator program in java using awt. 



Find details of Life Cycle of Applet in this link: https://www.comrevo.com/2018/10/life-cycle-of-applet.html.

Find Java Applet Life Cycle Program Example in following link:
https://www.comrevo.com/2019/08/Java-Applet-Life-Cycle-Program-Java-program-to-demonstrate-applet-life-cycle-Example.html 

Find Java Applet Hello World Program Example in following link:
https://www.comrevo.com/2019/08/Java-Applet-Hello-World-Example-Java-Applet-Hello-World-Program.html  


Watch following video ( Java Applet Tutorial | Java Applet Life Cycle | Hello World Applet Program in Java ) :

Explanation: 
                  Applet program does not have main() method. Hence, it can not be used for creating stand-alone application. It is to be used with other applications like webpages created by JSP, Servlet.
                  We need to extend class Applet given by package java.applet.
                  We can override init(), start(), stop(), destroy() methods given by Applet class. We may also need paint() method to redraw applet. paint() method is given by Component class which is given by java.awt package. paint() method accepts Graphics class object as parameter. 
                  To handle events, we need to implement ActionListener inteface. We need to add components TextFields, Labels, Buttons etc. on applet. Component (e.g. Button) from which we want to generate event has to be registered for event by calling addActionListener() method. We have to implement actionPerformed() method to handle event.

                  Go through the following programs:

Program (AppletCalculator.java)

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class AppletCalculator extends Applet implements ActionListener
{
   Label l1,l2,l3;
   TextField t1,t2;
   Button b1,b2,b3,b4;

   public void init()
   {                     
     setLayout(new FlowLayout());

     l1=new Label("First Number:");
     t1=new TextField(20);
    
     l2=new Label("Second Number:");
     t2=new TextField(20);
    
     b1=new Button("Add");
     b2=new Button("Sub");
     b3=new Button("Mul");
     b4=new Button("Div");

     l3=new Label("Result");
    
     add(l1);
     add(t1);
     add(l2);
     add(t2);
     add(b1);
     add(b2);
     add(b3);
     add(b4);
     add(l3);

     b1.addActionListener(this);
     b2.addActionListener(this);
     b3.addActionListener(this);
     b4.addActionListener(this);   
   }

   public void actionPerformed(ActionEvent ae)
   {
    Double num1=Double.parseDouble(t1.getText());
    Double num2=Double.parseDouble(t2.getText());
    if(ae.getSource()==b1)
     {
       Double value=num1+num2;
       l3.setText(""+value);
     }
    if(ae.getSource()==b2)
     {
       Double value=num1-num2;
       l3.setText(""+value);
     }
    if(ae.getSource()==b3)
     {
       Double value=num1*num2;
       l3.setText(""+value);
     }
    if(ae.getSource()==b4)
     {
       Double value=num1/num2;
       l3.setText(""+value);
     }
  }
}

Program (AppletCalculator.html)

<html>
<head><title>Applet Calculator</title></head>
<body>
<applet code = "AppletCalculator.class" width = "200" height = "400">
If your browser is Java enabled, then only you can see Applet in your browser. If it is not Java enabled,then you need to add Java plug-ins in your browser or use appletviewer to view applet.
</applet>
</body>  
</html>
  


 How to run from terminal
parag@parag-Inspiron-N4010:~/Desktop/programs/applet$ javac AppletCalculator.java
parag@parag-Inspiron-N4010:~/Desktop/programs/applet$ appletviewer
AppletCalculator.html
  

Output:
 

Find Life Cycle of Applet in following link:
https://www.comrevo.com/2018/10/life-cycle-of-applet.html .


No comments:

Post a Comment