prompt = new Label("Enter integer and press Enter:");
- creates a Label object, initializing the string literal to "Enter integer and press Enter:"
- The idea is to label the text field so that the user will know what to do: type in an integer and press the Enter key.
- We named it "prompt" because our intention is to prompt the user to act (in accordance with our wishes).
- The word new is an operator that requests enough memory from the Java runtime system to store a Label object;
Label("Enter integer and press Enter:")
- after the new operator, is used to construct our desired value for the Label object.
- It is called a constructor for Label objects. We learn more about constructors later.
- The = operator is an assignment operator: the value on its right is assigned to the name on its left: prompt (a name
that we declared would refer to an object of type Label).
input = new TextField(10);
- creates a TextField object (10 characters long) using the new operator, and assigns it to the reference input (a name
that we declared would refer to an object of type TextField).
- NOTE: Declaring an object indicates that we want a certain name to refer to a certain type of object.
- The object is not created until we apply the new operator.
add(prompt); // put prompt on applet
add(input); // put input on applet
invoke the add method to put the GUI components Label and TextField on the applet.
- Finally, the line
sum = 0;
assigns the value 0 to the sum variable.
- Unlike objects, memory for variables are allocated by virtue of their declaration.
- Every applet has an action method.
- It does nothing (as is the case with our HelloWorld applet), unless we specify it (as is the case with our Addition
applet).
- The action method enables the person who uses the applet to interact with the applet via the GUI components (e.g.,
buttons, and text fields).
- Such an interaction is called an event.
- Later, we more fully discuss the GUI components, and the event-driven model based on them.
- When the user of the Addition applet types into the text field, and presses enter or return, the action method is
executed with this event as one of its arguments.
public boolean action(Event e, Object o)
{
...
}
- indicates that the method is:
- public (explained later)
- returns a boolean value (i.e., either true or false),
- receives 2 arguments:
- an event named e
- an Object named o
- The event argument, e, is used by the applet to determine what event occurred.
- In some applets (not our Addition applet), many events can occur.
- In the Addition applet, the only event that can occur is when the user presses enter or return, while the cursor is in the
TextField.
- The Object, o, contains event-specific information.
- In our case, it contains the contents of the TextField when the user pressed enter or return.
- Since the information that o contains depends on the event, it can contain several different kinds of information.
- We want the String representation of o, so we apply the Object class's toString method to it.
- Think of Integer.parseInt as a method that takes a String as an argument, and returns an int as its value.
- (What actually is happening is a little different, to be explained later.)
- Finally, the value of the int is assigned to number, a name that we declared would refer to an int.
- This value remains the value of number until we change it.
- (What hppens if we type something into the TextField other than an integer?)
- The statement
sum = sum + number; //add number to sum
adds sum (initially 0) to the value of number, assigning the result to sum (destroying its old value).
- We want to display the new value of sum to the user of the Addition applet.
- To do this, we display the result in the status bar at the bottom of the appletviewer window (or browser window).
showStatus(Integer.toString(sum) );
- showStatus is a method of the Applet class that takes a String as its argument, and displays that string in the
status bar.
- The "method" Integer.toString(sum)takes an int as its argument (in this case sum), and returns its representation
as a String.
input.setText(""); // clear entry field
uses the setText method of the TextField class to set the input text field to the empty string (i.e., the String
with no characters in it.
- This clears the text that the user typed in the TextField named input.
return true;
specifies that the action method is to return a value of true.
- This tells the applet that the event (the user pressing enter) has been processed.
- The Addition applet has introduced some new objects and methods:
Applet
- void add( GUI component ) // put GUI component on the applet
- boolean action( Event, Object)
- void init()
- void showStatus( String ) // display String on status bar
Label
- void Label( String ) // constructs a label
- void setText( String )
TextField
- void TextField( int ) // construct a TextField that is int characters wide
Conversion "methods" (I'll explain later what they really are)
- int Integer.parseInt( String )
- String Integer.toString( int )