Copyright (c) SEMM NL All rights reserved.
Author : Paul Hamaker. Part of JavaLessons.com
Besides running standalone, the class AppletOrApp can run as an applet, too.
Running standalone requires a main method.
Using a Windows JDK, you could run this class in a DOS console window like this , thus starting its main method.
With Eclipse, you can select the source file name and then choose Run, Run As, Java Application.
In the main method an instance of the class is created .
It needs a Frame to live in, so this is created, too .
Our OwnFrame class extends the java.awt.Frame class.
A Frame is just what the name implies : it is a subclass of Container that represents the borders of a window that permit a window to be moved, resized, closed, contain a menubar, etc.
It usually contains another Component, in this case our AppletOrApp , where BorderLayout.CENTER means : frame should give the child all available space.
Because there's no browser to do it, we have to call the init method ourselves.
As a finishing touch, the frame is positioned at the center of the screen by setting its location based upon .....
the width and height of the screen ....
and those of the frame itself.
Not until now do we tell the frame with its companion AppletOrApp to show itself . A Frame is INVISIBLE by default !
In OwnFrame's contructor an instance of WindowAdapter is registered, to handle a window closing request from the user or the operating system.
Get rid of the frame and its contents.
End this program succesfully. Any other number than zero can be used as an error code for the OS. An ERRORLEVEL in a DOS batchfile/script, f.e.
If we run this application stand-alone like this, we can refer to 'uno' as args and to 'due' as args . args.length will be 2 .
args is a String array reference that's passed to the main method automatically. This is one way of passing parameters to a stand-alone app.
With Eclipse you can set the parameters by choosing Run, Run..., Java Application, 'ClassName', Arguments, Program arguments.
An application's classes plus its resources can be zipped into a jar for easy distribution. Starting it then amounts to simply doubleclicking the jar in a file explorer.
With Eclipse, the jar can be created by right-clicking the project name and choosing, Export, JAR file.
The jar must contain a META-INF/MANIFEST.MF with the application's entry point.
If you don't want a class that can be run as an applet, extend Panel instead.
Other interesting window methods are : windowIconified, windowDeiconified, windowActivated, windowDeactivated.
See G18.Adapters 1 in the GUI module .
When running stand-alone, don't call Applet-specific methods such as showStatus and getAppletContext, for example, because this will cause the program to fail, since no browser is present.
That is where this boolean could come in handy.
If the class runs as a real applet, inside a browser that is, the main method is completely ignored .
If the Applet-derived class contains a stop and/or destroy method, these are not called automatically when running stand-alone, so this has to be solved in a different way.
A Frame's default layout mode is the BorderLayout.