Java Reference
In-Depth Information
specify [package].[subpackage] ,inour case package its.SimpleFrame . Next,
we import class JFrame from the javax.swing library, so it becomes accessible
in our program ( import javax.swing.JFrame; ). We then specify the class name
SimpleFrame and that this class is derived from JFrame :
public class SimpleFrame extends JFrame
The constructor and the methods of SimpleFrame are now described in more
detail:
public SimpleFrame()
public void showIt()
public void showIt(String title)
public void showIt(String title, int x, int y)
public void hideIt()
SimpleFrame() augments the constructor of JFrame and sets the size to 200
200
pixels by calling setSize(200,200) in Line 9. Otherwise one would see only
×
!
the title bar because the content pane does not now contain anything. We also
set the position of the upper left corner of the frame to 200 pixels below the
upper edge of the screen and 200 pixels to the right. This is done by calling
setLocation(200,200) in Line 10. Note that the Java coordinate system is
upside-down: the positive y -axis points down; see also Figure 2.2. Finally the
constructor calls method setDefaultCloseOperation in Line 11 to guarantee
correct termination as described above. Note that keyword this in Lines 9 to 11
can be omitted. As mentioned in the introduction, we added it to make clear
that these statements refer to this frame.
showIt() makes the frame appear on the screen by calling setVisible with ar-
gument true .
showIt(String title) sets the title in the title bar to title and makes the frame
appear on the screen.
showIt(String title,int x,int y) sets the title in the title bar to title ,
changes the location to x , y , and makes the frame appear on the screen.
hideIt() makes the frame disappear from the screen by calling setVisible with
argument false . The frame is not destroyed by this command. The graphical
information is preserved and the frame can be made visible again without
calling a constructor.
To test the class SimpleFrame we use a driver program .Wemight have defined
a main -method in SimpleFrame for this purpose but we did not because we
shall use SimpleFrame as the basis for further applications. The driver class is
SimpleFrameDriver .Itgenerates two frames in Lines 7 and 8. The first frame
receives a title and is made visible in Line 9. The second one is in addition moved
Search WWH ::




Custom Search