Mimic:import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class Mimic extends Applet { Mimic_GUI gui = new Mimic_GUI (this); //=========================================================== // Initializes the applet by intializing the GUI. //=========================================================== public void init() { gui.init(); } //=========================================================== // Updates the label in the GUI with the text that was // entered in the text field. Also prints a message to // standard output indicating the action event. //=========================================================== public void update_label() { System.out.println ("Action"); gui.update_label (gui.get_quote()); } }
Mimic_GUI:import java.awt.*; public class Mimic_GUI { private Label label = new Label ("No news is good news."); private TextField quote = new TextField(20); private Mimic applet; private Mimic_Action_Listener listener; //=========================================================== // Sets up the GUI by creating a listener object. //=========================================================== public Mimic_GUI (Mimic mimic_applet) { applet = mimic_applet; listener = new Mimic_Action_Listener (applet); } //=========================================================== // Adds the components to the GUI and the listener to the // text field. //=========================================================== public void init() { applet.add (quote); applet.add (label); applet.resize (250, 100); // Add all associated listeners quote.addActionListener (listener); } //=========================================================== // Sets the text of the label. //=========================================================== public void update_label (String message) { label.setText (message); } //=========================================================== // Retrieves the text from the text field. //=========================================================== public String get_quote() { return quote.getText(); } }
Mimic_Action_Listener:import java.awt.event.*; public class Mimic_Action_Listener implements ActionListener { private Mimic applet; //=========================================================== // Sets up the listener by storing a reference to the // applet. //=========================================================== public Mimic_Action_Listener (Mimic listening_applet) { applet = listening_applet; } //=========================================================== // Updates the label when an Action event occurs. //=========================================================== public void actionPerformed (ActionEvent event) { applet.update_label(); } }