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
Thank you so much
ReplyDelete