Java
Java intro
Java basics
Java variables
Java conditionals
Java loops
Java arrays
Java OOP 1
Java OOP 2
Java interfaces
Java Strings
Java user input
Java exceptions
Java packages
Java rand numbers
Java GUI's
Java GUI layout
Java events
Java applets
Java graphics
Java sounds
Java summary

Programming

Programming intro

Markup

First webpage guide HTML
XHTML

Style & Layout

CSS

Browser scripting

Javascript
VBScript
AJAX

Server scripting

PHP
ASP

Making money online

Make money online

Java applets

An applet is a Java program that runs on a webpage within a web browser.

This lesson focuses on:

The Applet class

The Applet class is used to create applets. This class is located in the java.applet package. Applets are not created by instantiating an Applet object, rather by creating a class which extends the Applet class.

Example:

import java.applet.*;

class anApplet extends Applet{

}

In the above example, a class named anApplet that extends the Applet class is created. The java.applet package is imported into this class.

Methods of the Applet class

The Applet class has several methods:

Placing an applet on a webpage

An applet is placed on a webpage using HTML's <object> tag.

Example:

<object title="An applet" 
code="anApplet.class" width="200" height="200"></object>

In the above example, an applet named anApplet is placed on a webpage using the <applet> tag. The title attribute denotes a title to associate with the applet. The code attribute denotes the name and location of the applet. The height attribute denotes the height of the applet. The width attribute denotes the width of the applet.

Viewing an applet

You can view an applet by viewing the webpage on to which the applet is placed in a web browser or you can use the Java appletviewer command line tool to view it.

Syntax on the command line to view an applet:

appletviewer name_Of_Webpage_Where_Applet_Is_Located

Example:

appletviewer webpage.html

In the above example, the appletviewer utility is used to view a page named webpage.html, and if there is an applet on this page, then the appletviewer window will display it, otherwise nothing will be displayed.

Practice

Online code editor
Practical examples
Practical exercises
Step-by-step tutorials

Reference

Terms glossary
Reference material

Rate this site

Rate this site
Visitor comments