0% found this document useful (0 votes)
36 views4 pages

Week 10

An applet is a Java program that runs within a web browser. It has full access to the Java API and can function as a standalone application. An applet is created by writing Java code, compiling it, and embedding it within an HTML file using <applet> tags which specify attributes like the applet's width, height, and code file. Applets can be run by opening the HTML file in a browser or using the appletviewer testing tool. The JApplet class extends Applet and is used to create Swing-based applets with GUI components.

Uploaded by

L James Leonora
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views4 pages

Week 10

An applet is a Java program that runs within a web browser. It has full access to the Java API and can function as a standalone application. An applet is created by writing Java code, compiling it, and embedding it within an HTML file using <applet> tags which specify attributes like the applet's width, height, and code file. Applets can be run by opening the HTML file in a browser or using the appletviewer testing tool. The JApplet class extends Applet and is used to create Swing-based applets with GUI components.

Uploaded by

L James Leonora
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

JAVA APPLET

An applet is a Java program that runs in a Web browser. An applet can be a fully functional Java
application because it has the entire Java API at its disposal.

Applets

▪ 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.

▪ An applet is designed to be called from within another document written in HTML.

▪ When you create an applet, you do the following:

o Write the applet in Java, and save it with a .java file extension, just as when you write a
Java application.
o Compile the applet into bytecode using the javac command, just as when you write a
Java application.
o Write an HTML document that includes a statement to call your compiled Java class.
o 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.

▪ 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>.

▪ 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
<object code = ″AClass.class″
width = 300
height = 200>
</object>
Three attributes of the given object tag:
o  code = followed by the name of the compiled applet you are calling
o  width = followed by the width of the applet on the screen
o  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.

Example:
Example:
<html>
<applet
code = ″HelloApplet.class″
width = 400
height = 200
>
</applet>
</html>

Running an Applet
Two ways of running an applet:
o by opening the associated HTML file on a Webbrowser
o the appletviewer

NOTE: The appletviewer 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 a Simple Applet

▪ In creating an applet, you must also do the following:

o Include import statements to ensure that necessary classes are available.


o Learn to use some user interface (UI) components, such as buttons and text fields, and
applet methods.
o 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.

▪ 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.
A simple applet
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 an Applet with aninit() Method

▪ The Applet class provides four methods that are invoked by a Web browser when the browser
runs an applet.
o public void init()
o public void start()
o public void stop()
o public void destroy()

▪ If you fail to write one or more these methods, Java creates them for you.

▪ You can create an Applet using only the init() method.

Creating Multiple Components in a JApplet


import javax.swing.*;
import java.awt.*;
public class JHello extends JApplet {
Container con = getContentPane();
JLabel question = new JLabel(″What’s your
name?″);
JTextField answer = new JTextField(10);
JButton press = new JButton(″Press me″);
public void init() {
con.add(question);
con.add(answer);
con.add(press);
con.setLayout(new FlowLayout());}}

You might also like