Applications

Remember the HelloWorldApp.java

You might have noticed the main().

In the above, Java takes whatever is passed in as input and gives it to the main method as an array of Strings. These strings are the command-line arguments (as in C). You have access to any arguments that follow the application call. The user does not make the arguments Strings (i.e., you do not use "" for integers, etc.) you just pass whatever it is you want.

E.g.

(indexes in arrays start at 0)

java myprog arg1 arg2 arg3
(note: no extension of .class or .java)

note: in applets, you use the HTML attributes to pass arguments ... remember

<param name=image value="duke">
then in code use: getParameter("image")

main

A program needs to be started. In Java, the starting "class" needs a main() method.

When you run your Java application (javac to compile, java to run) the first thing that gets called is main().

The signature for main() must be:

public static void main(String arg[]) { }

The body of the main() method contains any code you need to get your application started - creating instances, etc.

When Java executes the main() method, keep in mind that main() is a class method - the class that holds it is not automatically instantiated when your program runs. If you want to treat that class as an object, you have to instantiate it in the main() method yourself.

also
An important thing to note about the arguments you pass to a java program is that those arguments will be stored in an array of strings. This means that any arguments you pass to your program will be converted to strings so they can be stored in the argument array. To treat them as non-strings, you'll need to convert them to whatever type you want them to be.

There must be exactly one main() in an application. Where is is? Could be in any class you put it.

A suggestion: (to be ontologically honest in OO)

Main() should really only be something to get the program going. For instance, maybe it opens a window or frame to instantiate a user-interface. In OO, one should always think of where methods should really reside...who is in charge of what. Since starting a system going is not really any certain classes responsibility, perhaps (for OO purposes and for your own book-keeping (where IS that stupid main() ) you might consider making a class called Main with one method main()

Then whenever you want to start a program, you call: java Main

Frames

See
java.awt.Frame or JFrame

In Java, a Frame is the base window used to create GUIs. In a typical browser, the Frame class is a parent class for the displayed GUI. (Frame is the parent of AppletViewer)

Frame extends Window (a blank window with no borders and no menubar)

Window is typically for pop-up kind of idea

Frame gives window a border, a layout, and a title.

The AppletViewer extends Frame to include a menubar and a drawing panel.

An example of the use of Frames: Memo Application

Java is a full object-oriented language (not just for applets) We will now look at how to do the same things we did with graphics for applets on Frames for applications.

A small application can be seen in the Memo Frame. The application will pop up a frame and display a message on the frame (the message can come from the command line, or it can be a default message (see Constructor)). If the user clicks in the window, the application will exit.

This class has two constructors Memo() and Memo(String s) - depending on if a variable is passed

Memo.java

You will also find pack() useful when you have multiple components:

pack(). Method in class java.awt.Window 
    Causes subcomponents of this window to be laid out 
    at their preferred size.

setVisible(boolean) - was hide() and show() makes the frame (and any attached components) visible

dispose() - frees up system resources occupied by frame only use when completely done with frame

Menus

Add items

Menu Events

When you select a menu item, the event generated is an ActionEvent. You need an ActionListener interface. Note the use of inner classes to implement the interface:

You can also add Checkbox menu items, division lines and submenus. Now, place it in the application's menu bar: Set menu bar for the frame All together now (1.1) and as seen in real life

***Note PopupMenu and MenuShotcut in ScribbleFrame (Nutshell2) http://www.ecst.csuchico.edu/~amk/foo/javanut2/index.html (example 8-1)

To create a popup menu, create a PopUpMenu, and add MenuItem objects to it. Do as you would with a regular menu pane. However, instead of adding a popupmenu to the menubar, you add it to the component over which it pops up. This is done with the add() method of the target component.

See also MenuShortcut

is a shortcut invoked as a Ctrl-C (platform independent ...cool). VK_ are Virtual Key constants defined in the KeyEvent class

Here is another example of an application (a decimal converter): download and run

Notice the main() - instantiates and starts