Home » Java Basics » 07 - A Simple GUI Application with Thread Management
7
Layout Managers
How to use layout managers to place components on a screen
A JApplet or JFrame component may contain many components which may in turn contain other components. The main component is called a container. The developer may have a very clear picture in mind about how he wants to position components on the container. He may select and use a layout manager to place the components on the screen.
- BorderLayout: A developer using this layout specifies one of five locations for each component: top, bottom, left, right, or center. More than one component may be placed in one area; they are positioned in the order of addition to the content pane. Often, only one or two of the areas of the BorderLayout — just the center, or center and top, are used.
- BoxLayout: A box layout places all components in a single column, one below the other OR in a single row. Alignment and spacing may be specified by the developer.
- CardLayout: A card layout is analogous to a pack of cards; only one is visible at a time. Tabbed panes - pop up dialogues on which you click on a tab to see its contents, are conceptually the same as the card layout. The card layout allows developers to specify an area in the container that contains different components at different times.
- FlowLayout: Specifying a layout is not compulsory; the components are arranged in a flowlayout by default. This layout simply places elements next to each other; if space runs out, a new row is started.
- GridLayout: The container is split into a number of cells of equal size based upon the number of rows and columns specified in the code. Each component is placed in a cell in the order of addition to the content pane.
- GridBagLayout: A gridbaglayout is more sophisticated than a GridLayout; the gridlayout is inflexible because all components are thrown into equal shaped cells; a grid bag holds components of different sizes, allows some components to span more than one row or column, and allows for custom padding and spacing.
- SpringLayout: A Spring layout works by allowing the developer to specify the exact amount of space (horizontal/vertical) between the edges of each component.
Our Applet uses a simple gridBagLayout to align the six labels and the custom imagePanel component.