1. Applet class
The Applet class is used without the main () method, cannot run independently, must be embedded in a Web page, and displays various information and effects on the Web page. So the application of applet class for Web-page interaction is more common. We generally refer to Java applet programs as Java applets
The Applet class is in the Java.applet package and must be loaded when it is used
The definition of the Applet class:
public class Applet extends Panel
2. Methods in the Apple class
Life cycle Methods:
public void init () is used for initialization, enabled when the applet is invoked, and only once
public void Start () to start running applets
public void Stop () applet
public void Destroy () closes the applet to clean up the resources invoked by the applet
Run State method:
Public boolean is Active ()
public string GetParameter (string name)
public void Showstatus (String msg)
...
3. Applet Program Example
Import java.awt.*;
Import java.applet.*;
public class Example extends applet{
String s;
public void init () {
s = "Welcome";
}
public void Paint (Graphics g) {
g.DrawString (S, 30, 30);
}
}
Embed applets in HTML
<HTML>
<HEAD>
<title>applet example</title>
</HEAD>
<applet Code = example.class width = height = 300></applet>
</HTML>
4. Passing messages to applets
Not only can we pass information from the applet to the browser, but we can also pass the message from the browser to the applet. In the Web page, we use Param to define the variable; in the applet, use GetParameter () to get the value of the variable
eg
Import java.awt.*;
Import java.applet.*;
public class Example extends applet{
String s;
public void init () {
s = getparameter ("P");
}
public void Paint (Graphics g) {
g.DrawString (S, 30, 30);
}
}
<HTML>
<HEAD>
<title>applet example</title>
</HEAD>
<applet Code=example.class width=300 height=300>
<param name=p value= "Did you see me?" >
</applet>
</HTML>
[Java] Applet