Java Reference
In-Depth Information
jf.setTitle("(" + jf.getX() + ", " + jf.getY() + ")");
But we cannot use the name
jf
because that variable name is not available
in method
setTitleToOrigin
. We need something more general, which can
refer to
this
instance, the object in which the method resides. Java uses the key-
word
this
for this purpose. So we put this statement in the body of
setTitleToOrigin
:
this
.setTitle("(" +
this
.getX() + ", " +
this
.getY() + ")");
Class
OurFrame
is in Fig. 1.8. Our sequence of statements now works!
OurFrame ourWindow=
new
OurFrame();
ourWindow.show();
ourWindow.setTitleToOrigin();
Compile class
ourFrame
in your IDE and then experiment with it. After execut-
ing the above statements, drag the window to a different place on your monitor
and then execute the call to method
setTitleToOrigin
again.
The manila folder for an instance of class OurFrame
Earlier, we showed how to draw an instance of a class as a manila folder,
with the name of the folder in the tab, the name of the class in a box in the upper
right, and the methods and fields in the folder itself. Drawing a folder of class
OurFrame
is slightly different because it extends class
JFrame
. An
OurFrame
manila folder has to have all the methods and fields that a
JFrame
has plus those
that are defined in
OurFrame
.
We show a manila folder of class
OurFrame
in Fig. 1.9. The folder has two
partitions. The top partition shows all the methods and fields that an instance of
superclass
JFrame
has. The bottom partition has the methods and fields that are
defined in subclass
OurFrame
. Of course, all the methods in both partitions are
available for use.
It is easy to remember how to draw such a folder. The superclass partition is
at the top, and the subclass (
sub
means
under
) partition is underneath the super-
class partition.
import
javax.swing.*;
public class
OurFrame
extends
JFrame {
/**
Set the title of this instance to contain the origin of the window */
public void
setTitleToOrigin() {
this
.setTitle("(" +
this
.getX() + ", " +
this
.getY() + ")");
}
}
Figure 1.8:
Class
OurFrame
, which is placed in file
OurFrame.java
Search WWH ::
Custom Search