Java Reference
In-Depth Information
Java syntax: Subclass definition
public class sub class-name extends superclass-name {
declaration of methods and fields
Style Note
13.2, 13.2.5
for classes
Purpose : To define a new file drawer, named subclass-name , and des-
cribe the contents of its manila folders (instances of the class). They
have the methods and fields that are defined in superclass superclass-
name as well as the methods and fields being defined in the subclass.
tion of its top-left corner). For example, if the origin is (5, 3) , we want to set
the title to "(5, 3)" . From Fig. 1.6, we see that procedure setTitle sets the
title. As we learned in Sec. 1.3, if we declare and initialize a JFrame variable jf ,
the following expression will produce the desired String :
"(" + jf.getX() + ", " + jf.getY() + ")"
Note that this is a catenation of three strings and two int s, and the two int s are
obtained by calling getter methods getX and getY . Using this expression, we can
set the title of the window like this:
jf.setTitle("(" + jf.getX() + ", " + jf.getY() + ")");
Try this statement in your IDE to make sure it works. Do not forget to import
the classes of package javax.swing and call method show .
Customizing JFrame: your first class, your first method
Every time we drag the window associate with a JFrame jf to a different
position, we want to fix the title in a simple manner. To do this, it would sure help
to be able to execute a method call that does this:
JFrame does not have this method. However, we can produce a customized ver-
sion by extending JFrame .
In the rest of this section, we define a new class called OurFrame (your first
class definition!) that has all JFrame 's methods and fields plus a new method,
setTitleToOrigin . In particular, we want to be able to execute the following
code, which should create an instance of OurFrame , show it, and set its title to its
OurFrame ourWindow= new OurFrame();;
OurFrame objects should behave just like JFrame objects, plus they should
have method setTitleToOrigin . Below is the outline of the definition of class
OurFrame , which must be placed in a file named :
Search WWH ::

Custom Search