/************************************************************** ** ** Program : window_ex.java ** ** Author : Ryan J. Stradling ** ** Created : August 3, 1996 ** ** Revisions : ** ** Purpose : This will allow the user to type in the input text ** field some text to be displayed in the output text field. ** The user can also press the DISPLAY MOM key to display the message ** "Hi, Mom!". ** ***************************************************************/ import java.awt.*; /********************************************************************* ** ** Class : window_ex ** ** Inherits : From the applet class ** ** This class will handle the window interactions and the game interface ** **********************************************************************/ public class window_ex extends java.applet.Applet { // Instance variables TextField input_text, output_text; // Textfields Button display_button; // Display button Label input_label, output_label; // Labels for textfields String out_string; // Entered string to output // Overridden init() method --------------------------------------- public void init () { // Initialize the window. setBackground(Color.blue); // Set background color to blue setForeground(Color.white); // Set foreground color to white setLayout(new FlowLayout()); // Set flow Style // Create the display button. display_button = new Button("DISPLAY MOM"); add (display_button); // Create input label and text field. input_label = new Label("Input: "); input_text = new TextField(15); add(input_label); add(input_text); // Create output label and text field. output_label = new Label("Output: "); add(output_label); output_text = new TextField(15); add(output_text); output_text.setEditable(false); // Set output field not editable } // end init() ------------------------------------------------------ // Overridden method action() ---------------------------------------- public boolean action (Event evt, Object arg) { // Handle the actions of the window. // Button hit if (evt.target instanceof Button) { DisplayText(evt); return true; } // Text field data entry else if (evt.target instanceof TextField) { if (evt.target == input_text) { out_string = input_text.getText(); DisplayEnteredText(); input_text.setText(""); return true; } } return false; } // end action() ---------------------------------------------------- // method DisplayTextEvent(Event) ------------------------------------ void DisplayText(Event evt) { // Display the text "Hi, Mom!" in the output field. if (evt.target == display_button) { output_text.setText("Hi, Mom!"); } } // end DisplayTextEvent(Event) -------------------------------------- // method DisplayEnteredText() ---------------------------------------- void DisplayEnteredText() { // Display the text the user entered. if (out_string != null) output_text.setText(out_string); } // end DisplayEnteredText() ------------------------------------------ }