If your UI class is a Frame
or Dialog
, you can
control its size and location at runtime. The size and location are
determined by what the code does when the UI window is created and what
the user does to resize or reposition it.
When the UI window is created, and various components are added to it,
each component that is added affects the preferredSize
of
the overall window, typically making the preferredSize
of
the window container larger as additional components are added. This
effect on preferredSize
depends on the layout manager of
the outer container, as well as any nested container layouts. For more
details about the way that preferredLayoutSize
is
calculated for various layouts, see the sections in this document on
each type of layout.
The size of the UI window, as set by your program (before any additional resizing that may be done by the user), is determined by which of the following container methods is called last in the code:
pack()
setSize()
The location of your UI at runtime will be
at 0,0 unless you override this by setting the location
property of the container (for example by calling setLocation()
before making it visible).
Sizing a Window Automatically with pack()
The pack()
method computes a window's preferredSize
, based upon the components it contains, and sizes itself accordingly.
pack()
creates the smallest possible window, while still
respecting the preferredSize
of the components that are
placed within it.
Note: The Application.java file created by the New
Application dialog calls pack()
, packing the frame to its
preferredSize
before making it visible.
How the preferredSize
is Calculated for a Container
preferredSize
is calculated differently for containers with
different layouts.
Portable Layouts
Portable layouts, such as FlowLayout
and BorderLayout
, calculate their preferredSize
based on a combination of the
layout rules and the preferredSize
of each component that
was added to the container. If any of the components are containers
(such as a Panel
), then the preferredSize
of
that Panel
is calculated according to its layout and
components. The layout calculation is recursed into as many layers of
nested containers as necessary. For more information about
preferredSize
calculation for particular layouts, see the
individual layout descriptions.
XYLayout
For XYLayout
containers, the preferredSize
of
the container is defined by the values specified in the width and height
properties of the XYLayout
. For example, if you have the
following lines of code in your container initialization,
xYLayoutN.setWidth(400);
xYLayoutN.setHeight(300);
and if xYLayoutN
is the layout manager for the container,
then its preferredSize is 400 x 300 pixels.
If one of the nested panels in your UI uses XYLayout
, that
panel's preferredSize will be determined by the layout's setWidth()
and setHeight()
calls. This value will be used for the panel in
computing the preferredSize
of the next (outer) container.
Explicitly Setting the Size of a Window Using setSize()
If you call setSize()
on the container (rather than
pack()
or subsequent to calling pack()
), the size of
the container will be set to a specific size, in pixels. setSize()
overrides the effect of pack()
and preferredSize
for the container.
Important: Because different screens have different
pixel sizes, if you use setSize()
you must call
validate()
in order for child containers to be properly laid out.
Note that pack()
automatically calls validate()
.
Making the Size of your UI Portable to Various Platforms
To make your UI portable, either use pack()
or calculate an
appropriate size to use with setSize( )
based on the pixel
sizes of the various screens your application will be deployed on.
For example, you might want the UI to appear at 75% of the width and
height of the screen. For the UI to appear at this sizing, you can add
the following lines of code to your application class instead of calling
pack()
:
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
frame.setSize(screenSize.width * 3 / 4, screenSize.height * 3 / 4);
Note: To ensure portability, change all XYLayout
containers to a portable layout after
prototyping.
Positioning a Window on the Screen
If you don't explicitly position your UI, it will appear in the upper left corner of the screen.
To center the UI on the screen, obtain the width and height of the screen, subtract the width and height of your UI, divide the difference by two (in order to create equal margins on opposite sides of the UI), and use these figures for the location of the upper left corner of your UI.
The following code is generated when you select the Center Frame on Screen option in the New Application dialog, and performs this calculation for you:
//Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height)
frameSize.height = screenSize.height;
if (frameSize.width > screenSize.width)
frameSize.width = screenSize.width;
frame.setLocation((screenSize.width- frameSize.width) / 2, (screenSize.height - frameSize.height) /2);
Placing the Sizing and Positioning Method Calls in your Code
The calls to pack()
, validate()
,
setSize()
, or setLocation()
can be made from inside
the UI container class for example, this.pack()
. These
calls can also be made from the class that creates the container, for
example, frame.pack()
, after invoking the constructor,
before setVisible()
. The latter is what the New Application
dialog-generated code does; the calls to pack()
or
validate()
, and setLocation()
are placed in the
Application class, after the frame is constructed (after the call to
jbInit()
).
If you are constructing the UI from various places within your
application, and you want it to come up in the same size and place,
place your calls in the constructor of your UI container class (after
the call to jbInit()
. If your application instantiates the
UI from only one place, such as in the New Application dialog generated
application, put the sizing and positioning code in the place where the
UI is created, in this case the Application class.
Copyright © 1997, 2004, Oracle. All rights reserved.