Java Reference
In-Depth Information
public Frame(GraphicsConfiguration gc). Creates a frame with the
specified GraphicsConfiguration of a screen device.
public Frame(String title, GraphicsConfiguration gc).
Creates a frame
with the specified title and GraphicsConfiguration.
Each of the preceding constructors creates a new Frame object that is ini-
tially invisible and has a size of 0 pixels wide and 0 pixels high. The String
passed in to a Frame constructor appears in the title bar, and the Graphics-
Configuration represents where the image is to be displayed. This is useful
when working with a multiscreen environment, but in most cases you do not
need to worry about a GraphicsConfiguration object. If you do not pass in a
GraphicsConfiguration object, your Frame will use the default graphics desti-
nation, which in Windows is the computer screen.
The following statement demonstrates instantiating a new Frame object in
memory:
Frame f = new Frame(“My first window”);
This Frame is not displayed on the screen, and it has an initial size of 0 by 0.
You need to give your Frame a size before displaying it, which can be done by
invoking one of the following five methods:
public void setSize(int width, int height).
Sets the size of the Frame to
the given width and height, in pixels.
public void setSize(java.awt.Dimension d). Sets the size of the Frame to
the same width and height as the given Dimension object.
public void setBounds(int x, int y, int width, int height). Sets both the
size and initial location of the window, where x represents the number
of pixels over from the upper-left corner of the screen, and y represents
the number of pixels down from the upper-left corner of the screen.
(See the sidebar titled GUI Coordinates .)
public void setBounds(java.awt.Rectangle r).
Sets the bounds of the
Frame to that of the given Rectangle.
public void pack(). Sets the size of the Frame to be just big enough to
display all its components with their preferred size.
I often notice students invoking more than one of the setSize(),
setBounds(), or pack() methods to set the size of a window. However,
you need to invoke only one of them. For example, if you invoke
setSize(200,200) and then setBounds(20,50,200,200), the first call to
setSize() was overridden by the subsequent call to setBounds(), making
the initial call to setSize() a waste of time. Similarly, invoking pack() and
then invoking setBounds() overrides whatever the pack() method did to
the size of the window.
Search WWH ::




Custom Search