Advanced GUI Applications Part II
- Contents
- Introduction to Applets
- Applet Limitations
- Introduction to HTML
- Document Structure Tags
- Text Formatting Tags
- Breaks in Text
- HTML Links
- Creating Applets With Swing
- Running an Applet
- Using
appletviewer - Java Applet Tutorial
Introduction to Applets
Java Applets
Applets are Java programs designed to work within a Web page.
If a user opens the Web page with a Java-enabled browser, the applet is displayed inside the browser window.
It appears to the user that the applet is part of the Web page.
Applets are stored on a Web server along with the site’s Web pages.
Applets associated with a viewed Web page are transmitted to the user’s system.
Once the applets are transmitted, the user’s system executes them.
Applets can be used to extend the capabilities of the Web page.
Hypertext Markup Language (HTML)
Web pages are transmitted to the user’s system as HTML.
HTML is static content, whereas Applets are dynamic.
They are stored and executed on the local computer.
Applet Limitations
For security purposes, by default, Applets cannot:
access the local computer file system.
run any other program on the user’s system.
retrieve certain information about the users or their systems.
make network connections with any system except the server from which the Applet was transmitted.
Introduction to HTML
HTML is the language of Web pages.
Hypertext can contain a link to other content on the Web page or another Web page.
A markup language allows designers to “mark up” a text file by inserting special instructions.
These instructions tell the browser how to format the text and create any hypertext links.
A Web page is often created as a text file.
It contains HTML instructions (known as tags).
It contains the text that should be displayed on the Web page.
It typically has a
.htmlfile extension.
The tags instruct the browser...
how to format the text
where to place images
what to do when the user clicks on a link.
Most HTML tags have an opening tag and a closing tag.
The tags are enclosed in angle brackets.
The closing tag is preceded by a forward slash.
For example:
<div>Some Text</div>
Document Structure Tags
The
htmltag marks the beginning and the ending of an HTML document.<html>The entire HTML document go here.</html>
The
headtag marks the document header, a section containing information about the document.The document header contains the
titletag, which defines the title of the page.Book example
Example code:
<html> <head> <title>Web Page Demo</title> </head> </html>After the document header comes the
bodytag.The document body contains tags and text that produce output in the browser.
Book example
Example code:
<html> <head> <title>Web Page Demo</title> </head> <body> <p>This is the first paragraph of this page.</p> <p>This is the second paragraph of this page.</p> </body> </html>
Text Formatting Tags
There are many HTML tags that you can use to change the appearance of text.
For example, there are six different heading tags.
<html> <head> <title>Web Page Demo</title> </head> <body> <h1>Header One</h1> <h2>Header Two</h2> <h3>Header Three</h3> <h4>Header Four</h4> <h5>Header Five</h5> <h6>Header Six</h6> </body> </html>A level one heading appears in boldface and is much larger than regular text.
A level two heading also appears in boldface but is smaller than a level one heading.
This pattern continues with the other heading tags.
Many tags allow an align attribute to modify where the text shows on the web page:
<h1 align="center">Header One</h1> <h1 align="left">Header One</h1> <h1 align="right">Header One</h1>
An old way of centering text is to use the
centertag to center a line of text.You can display text in boldface with tag
band italics with tagi.Book example:
Example:
<html> <head> <title>Web Page Demo</title> </head> <body> <h1 align="center">Header One</h1> <p>This is a paragraph under header one. <b>This line of text is bolded</b></p> <h2 align="center">Header Two</h2> <p>This is a paragraph under header two. <i>This line of text is italicsed</i></p> </body> </html>
Breaks in Text
The
hrtag causes a horizontal ruler to appear at the point in the text where it is inserted.A horizontal ruler is a thin, horizontal line that is drawn across the web page.
Book Example:
Example:
<html> <head> <title>Web Page Demo</title> </head> <body> <h1>Lorem Ipsum</h1> <p>Lorem ipsum dolor sit amet, anim id est laborum.</p> <hr/> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p> </body> </html>
HTML Links
A link is some element in a Web page that can be clicked by the user.
The tag that is used to insert a link has the following general format:
<a href="address">Text</a>
The
Textthat appears between the opening and the closing tags is the text that will be displayed in the Web page.The Web resource that is located at the
addresswill be displayed in the browser.This
addressis a uniform resource locator (URL).The
addressis enclosed in quotation marks.<a href="http://www.jade-cheng.com/">Click here to go to Jade's website</a>
Book Example:
Creating Applets With Swing
Applets are very similar to the Swing applications.
Instead of displaying its own window, an Applet appears in the browser’s window.
The differences between GUI application code and applet code are:
A Swing application window is commonly derived from
JFrame.An Applet class is derived from
JApplet.The
JAppletclass is part of thejavax.swingpackage.A Swing application commonly has a constructor that creates and initializes the GUI.
An Applet class does not normally have a constructor. Instead, it has a method named
initthat performs the same operations as a constructor.The following methods, common to Swing applications, are not called in an Applet.
supersetSizesetDefaultCloseOperationpacksetVisible
No
mainmethod is needed to create anAppletobject.The browser creates an instance of the class automatically.
Book Example:
Example code:
- Refer to Jade’s Applet Tutorial page.
Running an Applet
The process of running an Applet is different from that of running an application.
Applets are executed by creating an HTML document with an
applettag, which has the following general format:<applet code="Filename.class" archive="Filename.jar" width="pixel-width" height="pixel-height"> </applet>Attributes are enclosed in quotes.
Filename.classis the compiled byte-code of the class that extendsJApplet.The browser
downloads the specified Applet archive.
executes the Applet in an area that is the size specified by the
pixel-widthandpixel-height.
Example code:
- Refer to Jade’s Applet Tutorial page.
Using appletviewer
The
appletviewerprogram loads and executes an Applet without the need for a Web browser.When running the
appletviewerprogram, the name of an HTML document is specified as a command-line argument.$ appletviewer SimpleApplet.htmlThis command executes any Applet referenced by an
applettag in the fileSimpleApplet.html.If the document has more than one
applettag, it will execute each Applet in a separate window.
Java Applet Tutorial
Please visit my Java Applet Tutorial page.