Home Segments Top Top Previous Next

972: Mainline

Finally, you supply definitions for the methods that return minimum and preferred sizes. In the following example, the sizes are arbitrarily set such that the program displays small squares:

import java.awt.*; 
public class MovieApplicationLayout implements LayoutManager { 
 private Component meter; 
 private Component list; 
 private Component form; 
 private Component poster; 
 public void addLayoutComponent(String name, Component  o) { 
  if (name.equals("Meter")) {meter = o;} 
  else if (name.equals("List")) {list = o;} 
  else if (name.equals("Form")) {form = o;} 
  else if (name.equals("Poster")) {poster = o;} 
  else {System.err.println(name + " argument unrecognized");} 
 } 
 public void removeLayoutComponent(Component  o) { 
  if (meter == o) {meter = null;} 
  else if (list == o) {list = null;} 
  else if (form == o) {form = null;} 
  else if (poster == o) {poster = null;} 
 } 
 public void layoutContainer(Container parent) { 
  Dimension d = parent.getSize(); 
  int height = d.height; 
  int width = d.width; 
  if(meter != null) { 
   meter.setBounds(0, 0, (int) (width / 3),  
                   (int) (height * 2 / 3)); } 
  if(list != null) { 
   list.setBounds((int) (2 * width / 3), 0, 
                  (int) (width / 3), height); } 
  if(form != null) { 
   form.setBounds(0, (int) (height * 2 / 3),  
                  (int) (2 * width / 3), (int)(height / 3)); } 
  if(poster != null) { 
   poster.setBounds((int) (width / 3), 0,  
                    (int) (width / 3), (int) (height * 2 / 3)); } 
 } 
 public Dimension minimumLayoutSize(Container parent) { 
  return new Dimension(50, 50); 
 } 
 public Dimension preferredLayoutSize(Container parent) { 
  return new Dimension(50, 50); 
 } 
}