Previous | Next | Trail Map | Creating a User Interface | Table of Contents


Laying Out Components within a Container


Above are pictures of two programs, each of which displays five buttons. The Java code for both programs is almost identical. So why do they look so different? Because they use different layout managers to control the layout of the buttons.

This lesson tells you how to position the onscreen representations of Components. It'll show you how to use the layout managers the AWT provides. It'll also show you how to write your own layout manager. It'll even tell you how to do without a layout manager and use absolute positions. Finally, it'll discuss some common layout problems and solutions.

Using Layout Managers

Here's where to learn how to use layout managers. This section gives both general rules and detailed instructions on using each of the layout managers the AWT provides.

Creating a Custom Layout Manager

Instead of using one of the AWT's layout managers, you can write your own. Layout managers must implement the LayoutManager interface, which specifies the five methods every layout manager must define.

Doing Without a Layout Manager (Absolute Positioning)

You can position Components without using a layout manager. Generally, this solution is used to specify absolute positions for Components, and only for applications that are executed on only one platform. Absolute positioning is usually unsuitable for applets and other platform-independent programs, since the size of Components can be different on different platforms.

Common Layout Problems (and Their Solutions)


Previous | Next | Trail Map | Creating a User Interface | Table of Contents