The Component Hierarchy a Frame | ... | a Converter | ---------------------------------- | | a ConversionPanel (metricPanel) a ConversionPanel (usaPanel) | | ------------------- ------------------- | | | | | | a Label | a Choice a Label | a Choice | | -------------- -------------- | | | | a TextField a Scrollbar a TextField a Scrollbar Classes in the Example Program: The example program defines three classes. two classes that inherit from AWT classes: Converter and ConversionPanel are subclasses of Panel Panel: public class Panel extends Container Container: A generic Abstract Window Toolkit(AWT) container object is a component that can contain other AWT components. a data storing class: Unit Converter: data members: Frame window; ConversionPanel metricPanel, usaPanel; Unit metricDistances[] = new Unit[3]; Unit usaDistances[] = new Unit[4]; member functions: public void init() // Create the ConversionPanels void convert(ConversionPanel from) /* Does the conversion from metric to U.S., or vice versa, and * updates the appropriate ConversionPanel. */ public void paint(Graphics g) // Paints the component. Draws a box around this panel public Insets insets() /* Constructs and initializes a new Inset with the specified top, left, bottom, and right insets. Puts a little breathing space between the panel and its contents, which lets us draw a box in the paint() method. */ public static void main(String args[]) /** The Converter class is the heart of the example program. It contains the program's main() */ public static void main(String args[]) { Frame f = new Frame("Converter Applet/Application"); /* public class Frame extends Window Window(Frame): Constructs a new Window initialized to an invisible state. */ Converter converter = new Converter(); // constructor converter.init(); /* -initialize Converter: Converter has 2 ConversionPanels, one for Metric System, the other for U.S. System. -the two systems have different units: Centimeters, Meters, Kilometers and Inches Feet, Yards, Miles. */ f.add("Center", converter); /* add the specified component(converter) to the center of the container(frame) */ f.pack(); // Packs the components of the Window f.show(); /* Shows the Window. This will bring the window to the front if the window is already visible. */ } ConversionPanel: data members: String title; // for the label. Label label = new Label(title, Label.CENTER); TextField textField; Scrollbar slider; Choice unitChooser; int min = 0; int max = 10000; Converter controller; Unit units[]; member functions: ConversionPanel(Converter myController, String myTitle, Unit myUnits[]) /* the constructor for ConversionPanel. Set up default constraints Add the label Add the text field Add the pop-up list (Choice) Add the slider */ double getMultiplier() /* Returns the multiplier (units/meter) for the currently selected unit of measurement. */ public void paint(Graphics g) /* Paints the component. Draws a box around this panel. */ public Insets insets() /* Puts a little breathing space between the panel and its contents, which lets us draw a box in the paint() method. */ double getValue() /* Gets the current value in the text field. That's guaranteed to be the same as the value in the scroller (subject to rounding, of course). handleEvent() guarantees the above. */ /** Respond to user actions. */ public boolean handleEvent(Event e){ if (e.target instanceof Scrollbar) { textField.setText(String.valueOf(slider.getValue())); controller.convert(this); return true; } else if (e.target instanceof TextField) { setSliderValue(getValue()); controller.convert(this); return true; } else if (e.target instanceof Choice) { controller.convert(this); return true; } return false; } void setValue(double f) /* Set the values in the slider and text field. */ void setSliderValue(double f) /* Set the slider value. */ Unit: data members: String description; double multiplier; member functions: Unit(String description, double multiplier) // constructor public String toString()