This applet will display two text fields, with labels "Output: " and "Input: ",
and a button, labelled "DISPLAY MOM". If the user presses the button,
the "Output: " text field will display the message
"Hi, Mom!". If the user types text in the "Input: " text field and
hits the enter key, that text will be displayed in the "Output: " text field
and will be erased from the "Input: " text field.
We can implement this window as a Java applet by
import java.awt.*;
This statement imports the AWT library (package) to do the windowing in Java.
1: public class window_ex extends java.applet.Applet {
2:
3: // Instance variables
4:
5: TextField input_text, output_text; // Textfields
6: Button display_button; // Display button
7: Label input_label, output_label; // Labels for textfields
An init() method is inherited by all descendents of the Component class (including applets), but the inherited method does nothing useful so we override it with our own. In our init() method, we will want to set the background and foreground colors, display the "DISPLAY MOM" button, and display the two text fields.
First, we are going to set the background to blue and the foreground (the drawing color) to white. Then, we will set up a layout manager, which handles how the widgets are going to be displayed and where they are going to be displayed. Next, we will create a button, create the labels, and create the two text fields. Finally, we will add these widgets to the layout manager to be displayed.
Let's look at the source code for the init() method:
1: // Overridden init() method
2: public void init () {
3: // Initialize the window.
4: setBackground(Color.blue); // Set background color to blue.
5: setForeground(Color.white); // Set foreground color to white.
6:
7: setLayout(new FlowLayout()); // Set flow style.
8:
9: // Create the display button.
10: display_button = new Button("DISPLAY MOM");
11: add (display_button);
12:
13: // Create input label and text field.
14: input_label = new Label("Input: ");
15: input_text = new TextField(15);
16: add(input_label);
17: add(input_text);
18:
19:
20: // Create output label and text field.
21: output_label = new Label("Output: ");
22: add(output_label);
23: output_text = new TextField(15);
24: add(output_text);
25: output_text.setEditable(false); // Make output field not editable.
26:
27:
28: }
Line 10 creates display_button as a Button object with the label "DISPLAY MOM" (but does not display it). Line 11 causes the button to be displayed by using the add() method to send the button to the layout manager for display.
Line 14 will create a label with text "Input: " to identify the input text field on the applet. The next line, input_text = new TextField(15);, creates a text field of length 15 for the input text. To display the items (the label and the text field), the add() method is used in lines 16 and 17 to send the label and text field items to the layout manager to display. Next, the output_text text field and its label are created and displayed in the same manner. Finally, line 25 sets the output_text text field to be not editable, which means the user cannot type any text into it, by using the setEditable() method.
Event handling is discussed in further detail later in the Handling Events section.
1: public boolean action (Event evt, Object arg) {
2: // Handle the actions of the window.
3: // Button hit
4: if (evt.target instanceof Button) {
5: DisplayText(evt);
6: return true;
7: }
8: // Text field data entry
9: else if (evt.target instanceof TextField) {
10: if (evt.target == input_text) {
11: out_string = input_text.getText();
12: DisplayEnteredText();
13: input_text.setText("");
14: }
15: }
16: return false;
17: }
Line 11, out_string = input_text.getText(), gets the text entered by the user in the input_text text field. (A text field event occurs when the Enter key is pressed in the text field display.) The method DisplayEnteredText() is used to display the text from the input field in the output field. Line 13 clears the input_text text field.
void DisplayText(Event evt) {
// Display the text "Hi, Mom!" in the output field.
if (evt.target == display_button) {
output_text.setText("Hi, Mom!");
}
}
void DisplayEnteredText() {
// Display the text the user entered
if (out_string != null)
output_text.setText(out_string);
}
In this section, we created a simple interactive applet (window). In the
next section, we will start designing our Hilo game, which will build upon
the basic concepts that were illustrated in this section.