Glue

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page

Answer:

Yes ... usually you would use arithmetic expressions that use the current size of the frame to calculate a pleasing size for the spacers.

Glue

You want the spaces in your design to expand or shrink in proportion to the size of the frame. A fixed-sized component is inflexible. All GUI components have a getSize() method that is used to determine the dimensions of the component. The dimensions may change as a program executes, so this is a useful method. To determine the current size of a frame, use:

Dimension JFrame.getSize()
Glue

Then look at the height and width fields of the Dimension object. Use that data to create a rigid area of appropriate size. If this is too complicated, then use glue.

The glue component is another invisible component that class Box creates. Think of glue as stretchy caulking between components. It keeps them separated, but expands or shrinks as needed.

To create glue use one of the following:

Box.createHorizontalGlue()
Box.createVerticalGlue()

QUESTION 10:

How is a glue component added to container such as a frame?