LABORATORY 3: In-lab Exercise 3
Name
Hour/Period/Section Date
Strings can also be read and written in a Java GUI. A correct implementation of the DrawCurv program in Laboratory 2 (In-lab 2) generates a simple drawing of a set of points—the program “connects the dots.” A more interactive GUI that prints and accepts text will be developed in this lab exercise. In this process the console application of the Hangman program from In-lab 2 will be converted to a frame-based graphical application much like the DrawCurv program in Laboratory 2: In-lab 1 was converted to a graphical application in Laboratory 2: In-lab 2. First, it will not be necessary in this Hangman graphical application to import java.io.*, so remove that import statement from your program. In this program it is intended that all I/O will occur in the GUI. Only the java.awt import statements used in the DrawCurv program will be needed here.
The data members in the HangmanFrame class will include most of the variables used in the non-GUI Hangman implementation from In-lab 2 such as secretWord,guessLetter,numGuesses,
andguessTemplate plus the following graphic components:
private TextField guessedTxt, // for input of guessLetter
secretTxt; // for input of secretWord
private Label secretLbl, // message for secretWord text field
tempLbl, // displays the guessTemplate
guessLbl; // message for guessLetter text field
These graphic components will be defined and added to the frame in the constructor for the HangmanFrame class.
Several statements need to be included in the HangmanFrame constructor. Along with other basic frame setup statements like setTitle and setSize, include the setLayout method statement and its single argument as illustrated below.
// implement a simple layout similar to how text flows on a page setLayout(new FlowLayout( ));
The setLayout method governs how multiple components will be arranged in the Hangman frame. The FlowLayout class implements a simple layout style that is similar to the way in which words flow as you type them into a word processor. Flow layout puts as many compo- nents in a row as possible. When a component cannot fit in the current row, it is put on the next row. The alignment defaults to center. In this layout the order in which components are added
LABORATORY 3
71
The Label class is used to display a String in a GUI application. It defines three constants that can be used to control the alignment of the String: LEFT,CENTER, and RIGHT. A label is most often used for relaying a message to the program’s user. The following code fragment includes a Label declaration for a label that is centered and tells the user to “Enter the secret word”:
// Add a Label message to get the secret word.
secretLbl = new Label(" Enter the secret word: ", Label.CENTER); add(secretLbl);
// A manual 'trick' for moving next component to the next row toNextRow( );
Also included with the code fragment above is a call to toNextRow. This method ensures that the next component appears on the next row in the frame. The code for this method is provided for you in the file toNextRow.jshl. Our Hangman GUI has three labels: secretLbl, tempLbl, and
guessLbl. In order to reserve adequate space for the tempLbl label, declare it with a dummy string that is large enough to handle the longest template the program is likely to accept.
tempLbl = new Label("---", Label.CENTER);
Most graphical applications collect keyboard input through text fields. The TextField class creates a single-line of editable text in a GUI program. The text field appears on the screen as a white rectangular box. The program’s user can click on the box and type information that can be read by the application. Our Hangman GUI has two text fields, one for accepting the secret word (secretTxt) and one for entering the next letter guessed (guessedTxt). The TextField con- structor has one argument—an integer that specifies how many characters can be viewed in the text field. For example, the first text field in our Hangman GUI is declared as:
secretTxt = new TextField(15);
The user can actually type more than 15 characters, but only 15 characters of a longer string will be visible at one time. As with all components to be included in the window for this GUI, this text field must be added to the frame:
add(secretTxt):
When the user presses the Enter key inside a text field, the text field generates an action event. To capture each action event an ActionListener can be attached to each text field. For example, the following statement adds an action listener to our text field (secretTxt).
secretTxt.addActionListener(new MySecretListener( ));
The actionPerformed method in the inner class MySecretListener specifies the actions to be
performed when text is entered in text field secretTxt.
// An inner listener class for getting the secret word private class MySecretListener implements ActionListener {
{
// Get the secretWord; then clear and later remove it. secretWord = secretTxt.getText( );
secretTxt.setText(""); // Clear it: set to empty string
secretLbl.setText("GUESS MY SECRET WORD");
remove(secretTxt); // Remove text box so player can't see it
// Create the guessTemplate for the secretWord ...
// Enlarge the font for the template for emphasis tempLbl.setFont(new Font("Courier", Font.PLAIN, 18)); // setText in tempLbl to the created guessTemplate tempLbl.setText(guessTemplate.toString( ));
inTxtFld.requestFocus( ); // Place cursor in this TextField
// for receiving guessLetter input
}
} // inner class MySecretListener
In this actionPerformed method, the getText method returns the current contents of the text field as a String object. The setText method is used three times in this code fragment to reset
the contents of the text field (and labels) to the specified String argument. For instance,
secretTxt.setText("")clears the text field (or sets its text to the empty string) so that the user has a visual indication that the input has been processed. The remove(secretTxt) statement removes the specified component (secretTxt) from the invoking object (HangmanFrame) so the
Hangman player will never see the secretWord text field. For greater emphasis, the method
setFont is used to enlarge the size of the font for the Hangman template displayed by the Label
tempLbl. The setFont method used above takes one argument of type Font, and subsequently the Font constructor takes three parameters: the font face name as the String Courier, the style
as the Font constant Font.PLAIN, and the point size as 18. Finally, the requestFocus method does the user a favor by automatically placing the cursor in the guessedTxt text field waiting for the next guessed letter to be entered. The guessedTxt text field will also need an action listener that is defined as an inner class that implements the ActionListener along with its
actionPerformed method.
Step 1: Create a program that implements the two-person Hangman word-guessing game described in In-lab 2 as a graphical application or GUI. If necessary refer to Laboratory 2: In-lab 2, for additional help. Be sure to document your code.
Step 2: Complete the following test plan.
Step 3: Use the test plan you created in In-lab Exercise 2. Execute the test plan. If you dis- cover mistakes in your implementation of the Hangman game, correct them and execute the test plan again.