A HILO APPLICATION (NOT APPLET) PROGRAM


In this section of the tutorial, we will create a stand alone Java program. Rather than having to use a browser (or the appletviewer) to execute the program, we will run the program with the command "java {file}", where file is the name of the class.

OVERVIEW

In order to create this window, you will need the same widgets as in the applet hilo example. In addition, you will need to create a quit button to quit the applet. Also, the hilo class will no longer inherit from the Applet class. Instead the hilo class will inherit from the Frame class. The Frame class is the basic class for handling the displaying of windows. A snapshot of the window we are going to create for this example is shown below. If you have been following along in the tutorial, you will notice that this window looks the same as in the applet version with the addition of a quit button. (Note: That this is an image and not an applet, so it cannot be executed.)



You can look at the source code now if you wish.


ANALYSIS OF THE CODE

THE HILO CLASS

FROM THE CODE
public class hilo extends Frame {

Notice that we are no longer inheriting from the Applet class. Instead we are inheriting from the Frame class. The Frame class is the class that handles creating a window and handling its actions.

hilo() CONSTRUCTOR METHOD

If you look at the code, you can see that the init() method that was used in previous examples is no longer there. Instead setting up the widgets to be used is now contained in the hilo class constructor. The code is almost the same as the init() method used in the applet version with a few exceptions. First of all, notice that we must handle the sizing of the window (Frame) with the resize() method. We no longer use an html file to specify the height and width of the window. The line: resize(250, 400); will set the size of the window to be 250 pixels wide by 400 pixels tall. Also, notice that we have added a quit button because the program will be running stand alone, and we need a way to quit the program nicely. Finally, after we have put all the widgets we want on the window, we need to display the window. The line show(); will display the current object's window (Frame).

action() method

The action() method is similar to the applet version but now contains an event handler to handle the quit button being pressed.

CODE SNIPPET
1:                          else if (evt.target == quit_button) {
2:                            bet_text.disable();
3:                            dispose();
4:                            System.exit(0);
5:                            return true;
6:                         }


Line 1 tests to see if the quit button was pressed. Line 3 then invokes the Frame class's dispose() method to destroy the window. Finally, line 4 invokes the System.exit() method, which will exit the program.

main() METHOD

In order for a stand alone Java program to run, there must be a main() method in the class you want to run. Java requires the main() method to be declared public and static. In our example program, the line hilo hilo_game = new hilo(); creates an instance of the hilo class and will cause the program to start running and to display the window.

COMPILING AND RUNNING THE PROGRAM

Save the hilo.java program and compile it in the normal way ("javac hilo.java"). Next, to run the program type "java hilo" at the command line. This command will cause the class hilo to start running and a window similar to the one shown above to appear. (If your program is correct and Java is correctly installed on your system.)