import java.awt.*; import java.awt.event.*; import FieldModifier; import StringModifier; import MakeUpperCase; /** * A simple GUI application that permits the user to enter a string and * modify the string in simple ways. * * @author Samuel A. Rebelsky * @version 1.0 of March 1999 */ public class StringFun extends WindowAdapter implements ActionListener { // +----------------------------------------------------------- // | Fields | // +--------+ /** The frame that provides the central action of the program. */ protected Frame gui; /** The text field into which the user enters information. */ protected TextField info; /** The ``convert to uppercase'' button. */ protected Button upperCase; /** The quit button. */ protected Button quit; /** Some instructions. */ protected Label instructions; // +--------------+--------------------------------------------- // | Constructors | // +--------------+ /** * Build the graphical user interface and go! */ public StringFun() { // Create the frame that serves as the base of this application. gui = new Frame("Fun with strings"); gui.setLayout(new FlowLayout()); gui.addWindowListener(this); // Create the various components. instructions = new Label("Type in the field, and then click a modifier"); info = new TextField(" "); upperCase = new Button("Convert to UPPERCASE"); quit = new Button("Quit"); // Set up the listeners for the various components. upperCase.addActionListener( new FieldModifier(info, new MakeUpperCase())); quit.addActionListener(this); // Add the components to the frame. gui.add(instructions); gui.add(info); gui.add(upperCase); gui.add(quit); // Put the frame together. gui.pack(); // Clear the field. info.setText(""); // And go! gui.show(); } // StringFun() // +---------+------------------------------------------------- // | Methods | // +---------+ /** Quit the program. */ public void quit() { gui.dispose(); System.exit(0); } // quit() // +----------------+------------------------------------------ // | Event Handlers | // +----------------+ /** * When the user clicks the quit button (the only button that * this object listens for), quit the program. */ public void actionPerformed(ActionEvent evt) { String command = evt.getActionCommand(); if (command.equals("Quit")) quit(); else if (command.equals("")); else { // It's an error, but we do nothing. } } // actionPerformed(ActionEvent) /** * When the window closes, exit the program. */ public void windowClosing(WindowEvent evt) { quit(); } // windowClosing(WindowEvent) // +------+---------------------------------------------------- // | Main | // +------+ /** Build the GUI and go. */ public static void main(String[] args) { new StringFun(); } // main(String[]) } // class StringFun