Java Reference
In-Depth Information
a
x
b
a
b
y
Figure 2.2 The Java coordinate system is upside down. The origin (0,0) is the upper left
corner of the screen. The positive x -axis points right, the positive y -axis points downwards .If
embedding components into another one, e.g. into the content pane of a frame, then the
origin is the upper left corner of the parent component
pack() resizes the frame so that it tightly fits around components embedded into
its content pane.
setDefaultCloseOperation(int operation) determines what happens if the
'close' button of the frame is clicked. See the comments below.
Let us briefly discuss method setDefaultCloseOperation .Bydefault, the frame
becomes invisible when its 'close' button is clicked. The application that made
!
it visible is, however, still running, i.e. the program is not terminated. There are
some predefined constants in the class JFrame which can be used for operation .
We shall use JFrame.EXIT_ON_CLOSE .
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
Then the whole application is automatically terminated when the 'close' button
is clicked. Not adding this line would result in the following problem. Suppose
an application is started over and over again - as happens in the test phase of a
new program - and every time displays a new frame. Though the frames are all
made invisible by clicking the 'close' buttons, the applications are still running and
consuming resources. If many applications run in parallel and share the processor,
each of them is slowed down. Problems might also arise if all the free memory were
consumed.
Simply terminating an application by clicking the 'close' button is not always
a good idea. In general some cleaning up would be performed before exiting the
program. For example, one would save changes made to files, store data computed
or received while the program was running, etc. We shall see later in Section 9.3
how this can be achieved.
Let us apply our knowledge and create a first frame and display it. We shall
derive our own frame class from JFrame and add some new functions. Our class
is called SimpleFrame .Asitisderived from Swing class JFrame ,itinherits its
functionality. The constructor of a SimpleFrame is extended: it sets the size and
Search WWH ::




Custom Search