Java Reference
In-Depth Information
The main() method calls three methods for the aWindow object. The method setBounds() defines
the size and position of the window; the first pair of arguments correspond to the x and y coordinates of
the top-left corner of our application window relative to the top-left corner of the display screen, and the
second pair of arguments specify the width and height of the window in pixels. The screen coordinate
system has the origin point, (0, 0), at the top-left corner of the screen, with the positive x -axis running
left to right and the positive y -axis from top to bottom. The positive y -axis in screen coordinates is
therefore in the opposite direction to that of the usual Cartesian coordinate system.
50,100
X Axis
Measured in Pixels
Screen Coordinates
We have specified the top-left corner of our application window at position (50, 100) on the screen,
which will be 50 pixels to the right and 100 pixels down. Since the window will be 400 pixels wide and
150 pixels high, the bottom right corner will be at position (450, 250). The actual physical width and
height of the window as well as its position relative to the edge of the screen will depend on the size of
your screen and the display resolution. For a given screen size, the higher the display resolution, the
smaller the window will be and the closer it will be to the top left-hand corner, simply because the
pixels on the screen will be closer together. We will see how we can get around this potential problem
later in this chapter.
The setDefaultCloseOperation() method determines what happens when you close the window
by clicking on either the icon or selecting Close from the menu that is displayed when you click on
the in the top-left corner of the window. There are four possible argument values you can use here.
The constant we have used at the argument to the method, EXIT _ ON _ CLOSE , is defined in the JFrame
class. The effect of this is to close the window, dispose of the window resources and those of any
components it contains, and finally to terminate the application. There are three other argument values
you could use with the setDefaultCloseOperation() method that are defined in the
WindowConstants interface. These values are:
Argument
Description
DISPOSE _ ON _ CLOSE
This causes the frame and any components it contains to be
destroyed but doesn't terminate the application.
DO _ NOTHING _ ON _ CLOSE
This makes the close operation for the frame window
ineffective.
Search WWH ::




Custom Search