Sizing Frames

The size and position of any given frame is unpredictable unless you specify it. Specifying the size is easy. Just call the frame's setSize() method like this

f.setSize(150,150);

This size does not include the title bar so you'll need to account for that separately. To determine the height of a Frame's title bar call its insets() method and look at the top member of the resulting java.awt.Insets object. That will be the height of the title bar. That is,

int titleBarHeight = f.insets().top;

Moving the Frame to the proper place on the screen takes a little more effort. You move a Frame with the setLocation(int x, int y) method. However x and y are relative to the screen, not to the applet.

When a window is first created, it's invisible. Add components to the Frame while it's still invisible. The effect of buttons, labels and other widgets popping onto a layout in rapid succession while the window jumps around and changes size can be quite disconcerting. When you're done adding components, resizing and moving the Frame, make it visible by calling its setVisible() method like so:

f.setVisible(true);

You can resize a frame to the exact size necessary for what it holds using the pack() method:

f.pack()


Previous | Next | Top | Cafe au Lait

Copyright 1997, 1998, 2002 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified November 16, 2002