- Forward


GUI Layout
An Introduction with Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

GUI Layout
Back SMYC Forward
  • Recall:
    • GUIs consist of components and containers
  • Purpose of Layout:
    • Positioning components in containers (dynamically or statically)
Absolute Layout
Back SMYC Forward

The "Graph Paper" Approach

images/layout_absolute1.gif
Absolute Layout in Java
Back SMYC Forward

The null LayoutManager java.awt.LayoutManager

javaexamples/layout/NullLayoutDriver.java
 
Relative Layout
Back SMYC Forward
  • Defined:
    • Components are positioned relative to each other, rather than an absolute position
  • Pros and Cons:
    • Better than absolute layout across platforms
    • Can be fairly inflexible
Relative Layout in Java
Back SMYC Forward

Using FlowLayout java.awt.FlowLayout

javaexamples/layout/FlowLayoutDriver.java
 
Template Layout
Back SMYC Forward
  • Defined:
    • Divide the container into sections and place one component in each section
  • Pros and Cons:
    • Easy to use
    • The right template may not exist
Template Layout in Java (cont.)
Back SMYC Forward
  • Using GridLayout javax.swing.GridLayout :
    • Must specify the number of rows and columns in the grid
  • Sizing:
    • All cells are the same size
    • The cells are sized in such a way that they fill the available space
Template Layout in Java
Back SMYC Forward

Using GridLayout java.awt.GridLayout

javaexamples/layout/GridLayoutDriver.java
 
Template Layout in Java (cont.)
Back SMYC Forward
  • Using BoxLayout javax.swing.BoxLayout :
    • One row or column only
  • Fillers:
    • Struts (one-dimensional, fixed-size)
    • Rigid Areas (two-dimensional, fixed-size)
    • Glue (one-dimensional, variable-size)
Template Layout in Java (cont.)
Back SMYC Forward

Using BoxLayout javax.swing.BoxLayout

javaexamples/layout/BoxLayoutDriver.java
 
Template Layout in Java (cont.)
Back SMYC Forward

Using BorderLayout java.awt.BorderLayout

images/borderlayout.gif
Template Layout in Java (cont.)
Back SMYC Forward

A BorderLayout java.awt.BorderLayout Example

javaexamples/layout/BorderLayoutDriver.java
 
There's Always More to Learn
Back -