Program to create an applet to accept a username in the form of parameter and print 'Hello '

You can also pass parameters to an applet the below program shows the same. User defined parameters can be passed to applet by using <PARAM…> tag. Each <PARAM…> tag has a name attribute such as color and value attribute such as red. Parameters are passed to an applet when it get loaded.

init( ) method in applet can be defined to get control of parameters defined in the <PARAM…> tag. This can be done using getParameter( ) method, whose one string argument represents the name of parameter and returns a string containing the value of that parameter.

As shown in below program, we have passed Username as paramaeter name and RAHUL as parameter value in <PARAM…> tag. After that in init( ) method we have collected that parameter using getParameter( ) method which return the value as a string. If the parameter value is null then it will display "Java" otherwise it will display "Hello RAHUL" on applet window.


PROGRAM
import java.applet.Applet;
import java.awt.Graphics;

public class HelloJavaParam extends Applet{

 String msg="";
 
 public void init() 
 {
  
  msg = getParameter("Username");
  
  if(msg == null) 
  {
   msg = "Java";
  }
  
  msg = "Hello " + msg; 
  }
 
 public void paint(Graphics g)
 {
  g.drawString(msg,50,50);
 }
}

/* 
<applet code="HelloJavaParam" width=200 height=200>
<param name="Username" value="RAHUL">
</applet>
*/
OUTPUT

C:\>javac HelloJavaParam.java 
C:\>appletviewer HelloJavaParam.java



Comments

Post a comment

Popular posts from this blog

Program to define a class 'employee' with data members as empid, name and salary. Accept data for 5 objects using Array of objects and print it.

Define a class Student with four data members such as name, roll no.,sub1, and sub2. Define appropriate methods to initialize and display the values of data members. Also calculate total marks and percentage scored by student.

Program to draw Smiley in Applet