Chapter 24: Using AWT Controls, Layout Managers, and Menus 727
Sample output from theBorderLayoutDemoapplet is shown here:
Using Insets
Sometimes you will want to leave a small amount of space between the container that holds
your components and the window that contains it. To do this, override thegetInsets( )method
that is defined byContainer. This method returns anInsetsobject that contains the top,
bottom, left, and right inset to be used when the container is displayed. These values are
used by the layout manager to inset the components when it lays out the window. The
constructor forInsetsis shown here:
Insets(inttop, intleft, intbottom, intright)
The values passed intop, left, bottom,andrightspecify the amount of space between the
container and its enclosing window.
ThegetInsets( )method has this general form:
Insets getInsets( )
When overriding this method, you must return a newInsetsobject that contains the inset
spacing you desire.
Here is the precedingBorderLayoutexample modified so that it insets its components
ten pixels from each border. The background color has been set to cyan to help make the
insets more visible.
// Demonstrate BorderLayout with insets.
import java.awt.;
import java.applet.;
import java.util.;
/
*/
public class InsetsDemo extends Applet {
public void init() {
// set background color so insets can be easily seen