//====================================================================== // Project: CS 116, Method example // Author: Tom Kelliher // File: Methods.java // Purpose: The applet demonstrates the use of methods. A // Greetings class is designed and used. Two Greetings // objects are created and manipulated. //====================================================================== import java.applet.*; import java.awt.*; import java.awt.event.*; //====================================================================== // Class: Methods // Purpose: Create the Greetings objects and a few widgets // (label, buttons) for manipulating them. //====================================================================== public class Methods extends Applet implements ActionListener { private Greetings today = new Greetings(); private Greetings tomorrow = new Greetings(); // The current selected greeting will be displayed via this label. private Label greetingLabel = new Label("No greeting selected yet."); // Note that there is no Greeting corresponding to the yesterday // button, nor does actionPerformed() have the capability of // handling the button. This demonstrates that actionPerformed() // must be written to deal with each of the events that are // registered for it. private Button useYesterday = new Button("Yesterday"); private Button useToday = new Button("Today"); private Button useTomorrow = new Button("Tomorrow"); //=================================================================== // Method: init // Purpose: Initialize the applet and its widgets. //=================================================================== public void init() { // Buttons will go into this panel. Panel p = new Panel(new GridLayout(1, 3)); // Note that we're immediately changing today's greeting. today.setGreeting("Hi there!!!"); // greetingLabel will go over the buttons. setLayout(new GridLayout(2, 1)); // Set-up greetingLabel. add(greetingLabel); greetingLabel.setFont(new Font("Serif", Font.PLAIN, 20)); greetingLabel.setForeground(Color.green); greetingLabel.setBackground(Color.black); // Set-up the buttons. p.add(useYesterday); p.add(useToday); p.add(useTomorrow); add(p); useYesterday.setBackground(Color.magenta); useYesterday.addActionListener(this); useToday.setBackground(Color.red); useToday.addActionListener(this); useTomorrow.setBackground(Color.blue); useTomorrow.addActionListener(this); } //=================================================================== // Method: actionPerformed // Purpose: Handle button presses from useToday and useTomorrow // buttons. // Input: e, an actionEvent. //=================================================================== public void actionPerformed(ActionEvent e) { if (e.getActionCommand() == "Today") greetingLabel.setText(today.getGreeting()); else if (e.getActionCommand() == "Tomorrow") greetingLabel.setText(tomorrow.getGreeting()); else // We got an unexpected event. greetingLabel.setText("Huh???"); } } //====================================================================== // Class: Greetings // Purpose: This class holds a greeting in a string. The // default greeting is "Hello." Two methods are // provided: getGreeting() and setGreeting() (see // below for descriptions). //====================================================================== class Greetings { private String greeting; //=================================================================== // Method: Greetings constructor // Purpose: Initialize the greeting to the default. //=================================================================== public Greetings() { greeting = new String("Hello."); } //=================================================================== // Method: getGreeting // Purpose: Return the greeting string. // Return: The greeting string. //=================================================================== public String getGreeting() { return greeting; } //=================================================================== // Method: setGreeting // Purpose: Set the greeting string to newGreeting. // Input: newGreeting, a string. //=================================================================== public void setGreeting(String newGreeting) { greeting = newGreeting; // This demonstrates that the return statement can be used // anywhere, with or without a return value. This use is // redundant, since we're at the end of a void method. One will // usually only see the return statement when it's necessary to // return from the middle of a method and/or some sort of a value // is being returned. return; } }