160 likes | 374 Views
AWT Layout Managers (Chapter 10). Java Certification Study Group January 21, 1999 Mark Roth. GridLayout. BorderLayout. LayoutManager. FlowLayout. Layout Managers. Layout Managers. Removes repetitious need to lay out components precisely.
E N D
AWT Layout Managers(Chapter 10) Java Certification Study Group January 21, 1999 Mark Roth
GridLayout BorderLayout LayoutManager FlowLayout Layout Managers
Layout Managers • Removes repetitious need to lay out components precisely. • Insulates developer from the fact that components have different sizes when instantiated on different platforms. • All implement java.awt.LayoutManager interface.
Containers vs. Components • Container extends Component • Components are things like Button and Checkbox. • Containers contain Components
Hierarchy Sample(from Roberts, Figure 10.4) Frame Panel Panel Panel Panel Label Label Label Text Field Text Field Text Field Scrollbar Scrollbar Scrollbar Panel Button Button Button
Defaults • Panel = FlowLayout • Applet = FlowLayout • Frame = BorderLayout
Constructing a Panel • Construct the panel (new Panel()). • Give the panel a layout manager (setLayout()) • Populate the panel (add()) • Add the panel to its own container (add()).
Component Size and Position • Sizes and positions are merely suggestions, overridden by the LayoutManager. • Each Component has a preferred size, which is “just big enough” to display the component.
FlowLayout Manager • Default type for Panels and Applets. • A lot like a word processor: • Arranges components in horizontal rows. • Components “wrap” to the next line if they don’t fit across. • Honors a component’s preferred size. • Specify in constructor: • FlowLayout.LEFT, FlowLayout.CENTER, FlowLayout.RIGHT
GridLayout Manager • GridLayout( rows, columns ) • Forces a size on a component equal to the size of the cell. • add( component ) - Adds from left to right.
BorderLayout Manager “Center” “North” “West” “East” “South”
BorderLayout Manager • North and South • Honors preferred height • Forces width to width of container • East and West • Honors preferred width • Forces height to height of container. • Remainder of space given to center. • add( component, String )
“Find the Layout Manager” • Refer to 10.4 and find the following Layout Managers: • (1) BorderLayout • (4) GridLayout • (1) FlowLayout
Other Layout Managers • GridBag Layour Manager • Card Layout Manager • No Layout Manager • Create Your Own Layout Manager
References • All Material in this Presentation is heavily based on:Roberts, Simon and Heller, Philip, Java 1.1 Certification Study Guide, 1997: SYBEX™. ISBN: 0-7821-2069-5 • Selected portions from JDK 1.1.6 JavaDoc HTML pages.