站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > Java Tutorial 5.0 英文版

Defining an Applet Subclass - Java Tutorial 5.0 英文版

The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Deployment
Lesson: Applets

Defining an Applet Subclass

The first bold line of the following listing begins a block that defines the HelloWorld class.
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);
    }
}

The extends Keyword

The extends keyword indicates that HelloWorld is a subclass of the class whose name follows: JApplet. To learn more about subclasses and inheritance, see Object-Oriented Programming Concepts (in the Deployment trail).

Class Inheritance

Applets inherit a great deal of functionality from the Applet or JApplet class, including the abilities to communicate with the browser and to present a graphical user interface (GUI).

For example, when a Java-capable browser loads a page containing an applet, the browser sends a request to the applet, telling the applet to initialize itself and start executing. You'll learn more about what the Applet class provides in the rest of this lesson.

Applets and Multiple Classes

An applet must extend either Applet or JApplet. You can also write other classes to support the applet's functionality. Java Plug-in loads other classes from the same location from which it loaded the applet's main class.


Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.