100% found this document useful (1 vote)
168 views20 pages

OOPR212 Applet

The document discusses Java applets, including: 1. An applet is a small program designed to run within a larger program like a web browser, with limited features and requiring minimal resources. A Java applet is written in Java and designed to run from a web browser. 2. To create an applet, write the code in Java and save it with a .java extension, then include it in an HTML file using tags like <applet> or <object>. 3. The JApplet class is used to create Swing applets and extends the Applet class, providing a container and components. Its init() method is used to initialize an applet.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
168 views20 pages

OOPR212 Applet

The document discusses Java applets, including: 1. An applet is a small program designed to run within a larger program like a web browser, with limited features and requiring minimal resources. A Java applet is written in Java and designed to run from a web browser. 2. To create an applet, write the code in Java and save it with a .java extension, then include it in an HTML file using tags like <applet> or <object>. 3. The JApplet class is used to create Swing applets and extends the Applet class, providing a container and components. Its init() method is used to initialize an applet.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

OOPR212

Object Oriented
Programming II W/ Lab
Learning
Objectives
• Learn
Applet
Applet
• In general, an applet is a small program or utility with limited features, requiring
minimal resources, and usually designed to run within a larger program.

• A Java applet is such a program written in the Java programming language and
typically designed to run from a web browser.
Applet Creation
• Write the applet in Java, and save it with a .java file extension, just as when you
write a Java application.

• Write an HTML document that includes a statement to call your compiled Java
class.

• Load the HTML document into a Web browser (such as Mozilla Firefox or Microsoft
Internet Explorer), or open the HTML document using the Applet Viewer.
Applet Creation
• The tag that begins every HTML document is <html>, which is surrounded by
angle brackets.

• The html within the tag is an HTML keyword that specifies that an HTML document
follows the keyword.

• The tag that ends every HTML document is </html>.


Applet Creation
• Generally, HTML documents contain other tags with the texts. Of importance at this point
is the <object>-</object> tag pair used to run an applet from within an HTML document.

• Three attributes (or arguments) are placed within the <object> tag: code, width, and
height.

• Example
<object code = ″Oopr212.class″ width = 300 height = 200>
</object>
Applet Creation
Three (3) attributes of object tag

• code - followed by the name of the compiled applet you are calling

• width - followed by the width of the applet on the screen

• height - followed by the height of the applet on the screen.

NOTE: Instead of the <object> and </object> tag pair, you can use the tag
set <applet> and </applet> in your HTML applet host documents.
Applet Creation
<html>

<applet code = ″HelloApplet.class″ width = 400 height = 200>

</applet>

</html>

NOTE: Instead of the <object> and </object> tag pair, you can use the tag
set <applet> and </applet> in your HTML applet host documents.
Running an
Applet
• Two ways of running an applet:
• By opening the associated HTML file on a Web browser
• the applet viewer.

NOTE: The applet viewer is used mainly for testing applets. It is, in effect, a web browser
that recognizes only the tags associated with the execution of an applet.
Writing an
Applet
• In creating an applet, you must also do the following:
• Include import statements to ensure that necessary classes are available.
• Learn to use some user interface (UI) components, such as buttons and text fields, and applet
methods.
• Learn to use the keyword extends.

• Creators of Java created an applet class named JApplet that you can import using the
statement.
• import javax.swing.JApplet;
JApplet Class
• JApplet is the Swing equivalent of the AWT Applet class.

• JApplet is a simple extension of java.applet.Applet for use when creating Swing


programs designed to be used in a Web browser.

• As a direct subclass of Applet, JApplet is used in much the same way as the Applet.

• A JApplet is a Component, and it is also a Container.


JApplet Class
• Inheritance hierarchy of the JApplet class
JApplet Class
• When using the JApplet class, you need the following import statements:
• import javax.swing.*;

• import java.awt.*;

• import java.awt.event.*;

• The extends keyword indicates that your applet builds on, or inherits, the traits of
the JApplet class.
JApplet Class
import javax.swing.*;
import java.awt.*;

public class HelloApplet extends JApplet {


public void init() {
Container con = getContentPane();
cont.setLayout(new FlowLayout());
JLabel label = new JLabel("Hello world!"); con.add(label);
}
Creating applet
init()
• The Applet class provides four methods that are invoked by a Web browser
when the browser runs an applet.
• Public void init()
• Public void start()
• Public void stop()

Note:
• Public void destroy()If you fail to write one or more these methods, Java
creates them for you.
• You can create an Applet usingonly the init() method.
Creating applet
init()
• The Applet class provides four methods that are invoked by a Web browser
when the browser runs an applet.
• Public void init()
• Public void start()
• Public void stop()
Creating applet
init()
Handling event
• To respond to user events within any Applet
• you create, you must do the following:
• Prepare your Applet to accept event messages.
• Tell your Applet to expect events to happen.
• Tell your Applet how to respond to the events.
Handling event
Applet Life
Cycle
There are four life cycle methods of an Applet class on which any applet is
built.
• init(): This method is called to initialize an applet.
• start(): This method is called after the initialization of the applet.
• stop(): This method is automatically called whenever the user moves away from the
page containing applets.
• destroy(): This method is only called when the browser shuts down normally.

You might also like