/* * @file: PanelTest2.java * @source: David Geary [Graphic Java, p.490] * * @purpose: * This applet illustrate the basic layout of * components to achieve a simple GUI for some * hypothetical application. * * The class WorkPanel is our focus: * this panel has two subpanels called "centerPanel" * and "controlPanel". These subpanels are laid out at the center and * south, respectively, using the BorderLayout() strategy. * * The WorkPanel() constructor accepts a Panel p as argument, * and p becomes its "centerPanel". * There is a method "addButton(String)" to add buttons to the * controlPanel. * * The class PanelTest2 is an applet that constructs a * WorkPanel (called "workPanel"). * * @history: * Adapted by Chee Yap for * Visualization Class, Spring 2003 */ import java.applet.Applet; import java.awt.*; // WorkPanel is THE CLASS to do all the work: // It is called by the Applet Class "PanelTest2" below. class WorkPanel extends Panel { Panel centerPanel; // the actual panel is an argument to constructor Panel controlPanel = new Panel(); // Constructor: public WorkPanel (Panel p) { this.centerPanel = p; // initialize centerPanel setLayout(new BorderLayout()); add(centerPanel, "Center"); add(controlPanel, "South"); } // Method: this adds buttons to controlPanel public void addButton(String label) { controlPanel.add(new Button(label)); } }//class WorkPanel // THIS IS THE APPLET CLASS WHICH USES THE WorkPanel Class above: public class PanelTest2 extends Applet { // init: public void init() { Panel cPanel = new Panel(); WorkPanel wPanel = new WorkPanel(cPanel); // put cPanel in wPanel // add some functions to the controlPanel wPanel.addButton("Quit"); wPanel.addButton("Update"); // add some functions to the centerPanel cPanel.add(new Label("Entry:")); cPanel.add(new TextField(25)); cPanel.add(new Label("Old Entry: (BLANK)")); setLayout(new BorderLayout()); // layout for this applet add(wPanel); // add wPanel to our applet } }//class PanelTest2 /********************************************************************** EXERCISE 1: Draw borders around each of the 2 panels. You will need the getSize() method. EXERCISE 2: The current buttons don't do anything. Make the "Quit" button cause the applet to terminate. Make the "Update" button read the text in the TextField, and update the label "Old Entry: (BLANK)" to "Old Entry: ..." where "..." is the text entered. **********************************************************************/