In order to make the GUI for the Hilo game useful, we need to specify what actions are to be performed whenever the DEAL button is clicked and when text is typed into the "Your Bet:" text field. An action such as a mouse click or a carriage return (enter) in a text field causes an event to occur in a Java program.
Click here on hilo game if you wish to review the Hilo game and its GUI. You can also review the source code that we wrote previously for the hilo_win class, which created the GUI for the Hilo game.
The action() method for an applet must be declared public and must return a boolean value as to whether or not an action has occurred (i.e., whether the event has been handled). The method returns true to indicate that it has successfully handled the action or false if the event that triggered the action should be passed up to the component's (widget's) parent. The action() method receives two parameters: an Event instance and an Object instance. The Event parameter is the event that occurred, while the Object parameter can provide additional information such as the label (name) of a button that was pressed. Rather than further general discussion, let's look at the action() method for our Hilo applet:
1: public boolean action (Event evt, Object arg) { 2: /* Deal with the actions of the window */ 3: // Deal button hit 4: if (evt.target instanceof Button) { 5: if (evt.target == deal_button) { 6: deal_button.disable(); 7: deal(); 8: return true; 9: } 10: return false; 11: } 12: // Text field had data entered into it 13: else if (evt.target instanceof TextField) { 14: if (evt.target == bet_text) { 15: bet_text.disable(); 16: bet(); 17: return true; 18: } 19: return false; 20: } 21: return false; 22: }
In our program we have only one possible button that could be pressed, but let's still look at how to determine which one was pressed when we have a program with more than one button. A way to do this is illustrated in line 5: if (evt.target == deal_button) {. (This test could be omitted in our Hilo applet, although some would argue that it is good programming practice to include it in case other buttons are added later.)
Lines 6-8 are the actions for a click on the Deal buton. The button is first disabled to prevent another click, and the deal() method is applied (to the applet). (The deal() method will be discussed in a later section.)
Now let's take a look at line 14: if (evt.target == bet_text) {. Line 13 determined whether the event occurred on a Textfield object, and in line 14 we determine whether the event occurred on the bet_text text field. In our Hilo GUI window the only text field where an event can occur is on the bet_text text field, because it is the only text field that allows user input. (It is the only text field to which setEditable(false) was not applied.) So this test could be omitted in our hilo_win action() method, but it is included here to illustrate how the determination could be made in a program with more than one editable text field.
The actions for a bet_text event are similar to those for a Deal button event. The text field is disabled to prevent further input, and the bet() method is used to perform the other actions. (The bet() method will be discussed in a later section.)
We are now ready to complete the Hilo applet, and we begin by looking at its design in the next section.