Layering a Screen

Published

This guide assumes that you have already read Drawing a Screen.

JLayeredPane Creation

As defined in the documentation, a "JLayeredPane adds depth to a JFC/Swing container, allowing components to overlap each other when needed. An Integer object specifies each component's depth in the container, where higher-numbered components sit 'on top' of other components.".

A JLayeredPane can be created as follows.

When adding a component to the JLayerePane, you must specify the layer as an Integer object and not an int primitive.

Opaque Components

All Swing components have access to the setOpaque() method. Most components have this set to false by default. As the documentation states, "If true the component paints every pixel within its bounds. Otherwise, the component may not paint some or all of its pixels, allowing the underlying pixels to show through.".

Layering Screens

Using the information above, you should now be able to understand the following examples. The only difference between the two is that the second example setsĀ topPanel as opaque.

A visual example of the preceding code.

A visual example of the preceding code.

Further Reading