Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Below is the HelloWorld applet, which is a simple Java class that prints the string "Hello World" in small rectangle.
Note: If you don't see the applet running above, you need to install Java Plug-in, which happens automatically when you install the J2SE JRE or JDK. This applet requires JDK 5.0 or later. You can find more information in the Java Plug-in home page.If you don't see the applet running above, you need to install Java Plug-in, which happens automatically when you install the JRE or JDK.
We strongly recommend that you install the latest version; at least 1.4.2 is required for all our applets. You can find more information in Troubleshooting Applet Problems
Following is the source code for the HelloWorld applet:
import javax.swing.JApplet; import java.awt.Graphics; public class HelloWorld extends JApplet { public void paint(Graphics g) { g.drawRect(0, 0, getSize().width - 1, getSize().height - 1); g.drawString("Hello world!", 5, 15); } }An applet such as this is typically managed and run by Java Plug-in. Java Plug-in, which is automatically included when you download the Java Runtime Environment (JRE), extends the functionality of a web browser, allowing applets to be run under Sun's Java 2 runtime environment (JRE) rather than the Java runtime environment that comes with the web browser. It works with the Mozilla family of browsers and with Internet Explorer.
The rest of this section contains the following to help you get started with applets:
The code above starts off with two import
statements, so you can reference the classes needed to build the applet.
Every applet must define a subclass of theApplet
orJApplet
class. In the "Hello World" applet, this subclass is calledHelloWorld
. Applets inherit a great deal of functionality from theApplet
orJApplet
class, ranging from communication with the browser to the ability to present a graphical user interface (GUI).
The HelloWorld applet implements just one method, thepaint
method. Every applet must implement at least one of the following methods:init
,start
, orpaint
. This section introduces a new applet,Simple
, that uses all of these methods. Unlike Java applications, applets do not need to implement amain
method.
The JApplet
class provides a framework for applet execution, defining methods that the system calls when milestones -- major events in an applet's life cycle -- occur. Most applets override some or all of these methods to respond appropriately to milestones.
Applets inherit the drawing and event handling methods of the AWT Component
class. Drawing refers to anything related to representing an applet on-screen -- drawing images, presenting user interface components such as buttons, or using graphics primitives. Event handling refers to detecting and processing user input such as mouse clicks and key presses, as well as more abstract events such as saving files and iconifying windows.
Applets inherit from the AWTContainer
class. This means that they are designed to holdComponents
-- user interface objects such as buttons, labels, pop-up lists, and scrollbars. Like otherContainers
, applets use layout managers to control the positioning ofComponents
.
For security reasons, applets that are loaded over the network have several restrictions. One is that an applet can't ordinarily read or write files on the computer that it's executing on. Another is that an applet can't make network connections except to the host that it came from. Despite these restrictions, applets can do some things that you might not expect. For example, applets can invoke the public methods of other applets on the same page.
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.