[Java] Applet

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.