Java Reference
In-Depth Information
TABLE 6-1 Some Methods Provided by the class JFrame (continued)
Method / Description / Example
public void addWindowListener(WindowEvent e)
//Method to register a window listener object to a JFrame.
The class JFrame also contains methods to set the color of a window. Chapter 12
describes these methods.
There are two ways to make an application program create a window. The first way is to
declare an object of type JFrame , instantiate the object, and then use various methods to
manipulate the window. In this case, the object created can use the various applicable
methods of the class.
The second way is to create the class containing the application program by extending the
definition of the class JFrame ; that is, the class containing the application program is
built ''on top of'' the class JFrame . In Java, this way of creating a class uses the
mechanism of inheritance. Inheritance means that a new class can be derived from or
based on an already existing class. The new class ''inherits'' features such as methods from
the existing class, which saves a lot of time for programmers. For example, we could
define a new class RectangleProgram that would extend the definition of JFrame .
The class RectangleProgram would be able to use the variables and methods from
JFrame , and also add some functionality of its own (such as the ability to calculate the
area and perimeter of a rectangle).
When you use inheritance, the class containing your application program will have more
than one method. In addition to the method main , you will have at least one other
method that will be used to create a window object containing the required GUI
components (such as labels and text fields). This additional method is a special type of
method called a constructor. A constructor is a method of a class that is automatically
executed when an object of the class is created. Typically, a constructor is used to
initialize an object. The name of the constructor is always the same as the name of the
class. For example, the constructor for the class RectangleProgram would be named
RectangleProgram .
Chapter 10 discusses the principles of inheritance in detail. Constructors are covered in
detail in Chapter 8.
Because inheritance is an important concept in programming languages such as Java, we
will use the second way of creating a window. We will extend the definition of the
class JFrame by using the modifier extends . For example, the definition of the class
Search WWH ::




Custom Search