Lab8: Visual Layout
Most of the codes and activities source: Introduction to Programming Using Java, Sixth EditionWhat is laying out componnets on the screen?
- Deciding where they are.
- Deciding how big they are.
FlowLayout
A FlowLayout simply lines up components in a row across the container. The size of each component is equal to that component's "preferred size." After laying out as many items as will fit in a row across the container, the layout manager will move on to the next row. The components in a given row can be either left-aligned, right-aligned, or centered within that row, and there can be horizontal and vertical gaps between components. If the default constructor, "new FlowLayout()", is used, then the components on each row will be centered and both the horizontal and the vertical gaps will be five pixels. The constructor:public FlowLayout(int align, int hgap, int vgap)can be used to specify alternative alignment and gaps. The possible values of align are:
FlowLayout.LEFT, FlowLayout.RIGHT, and FlowLayout.CENTER.
Example
content.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
BorderLayout
A BorderLayout layout manager is designed to display one large, central component, with up to four smaller components arranged along the edges of the central component.
content.add( comp, borderLayoutPosition );A BorderLayout
selects the sizes of its components as follows:
- The NORTH and SOUTH components (if present) are shown at their preferred heights, but their width is set equal to the full width of the container.
- The EAST and WEST components are shown at their preferred widths, but their height is set to the height of the container, minus the space occupied by the NORTH and SOUTH components.
- The CENTER component takes up any remaining space. The preferred size of the CENTER component is ignored when the layout is done, but it is taken into account when the preferred size of the container is computed.
GridLayout
A grid layout lays out components in a grid containing rows and columns of equal sized rectangles.
Components are added to the grid in the order shown; that is, each
row is filled from left to right before going on the next row.
new GridLayout(number of Rows, number of Columns)Example:
content.setLayout(new GridLayout(0,2));
content.setLayout(new GridLayout(0,1));
content.setLayout(new GridLayout(0,1,5,5));
Combining Layouts in a Tabbed Pane area
Tabbaed Pane is a class that manages two or more components (usually JPanel instances) that share the same display space.
JTabbedPane tabbedPane = new JTabbedPane();
JPanel tab1 = new JPanel();
tab1.setLayout(new GridLayout(1,0,10,10));
....// add your components here
JPanel tab2 = new JPanel();
tab2.setLayout(new FlowLayout());
....// add your components here
tabbedPane.add("GridLayout", tab1);
tabbedPane.add("FlowLayout",tab2);
JPanel mainContent= new JPanel();
mainContent.add(tabbedPane);
Combining Layouts in a Tabbed Pane area and layout inside layout
The First tab in the example below uses FlowLayout. Then inside one JPanel it used BorderLayout inside one panel.To see more layout managers: A Visual Guide to Layout Managers