java.awt and java.applet Packages

Original design goals:

Problems with the AWT:


Applet Introduction

What is an applet?

Life cycle of the applet:

Standard applet methods to override:

See the Simple.java applet for an illustration of the applet life cycle.

Other applet methods to invoke:


Applet Tag

<APPLET>
Specifies an applet to run within a web document. A browser that does not support applets displays the alternate text.

<CODE>
Required. It specifies the name of the class file that contains the compiled Java code. It must be relative to the code base. May be replaced by the OBJECT attribute under JDK 1.1.

<WIDTH>
Required. Initial width in pixels of the applet in the browser window.

<HEIGHT>
Required. Initial height in pixels of the applet in the browser window.

<OBJECT>
As of JDK 1.1, specifies the name of a file containing a serialized applet. Note that the "init" method of the applet is called. Thus, the applet should be initialized but not started before it is serialized.

<ARCHIVE>
Optional. As of JDK 1.1, a comma-separated list of JAR files to be loaded. These files may contain class files, images, sound files, etc. Note that the local disk is searched first.

<CODEBASE>
Optional. Base URL (absolute or relative) of the applet to be displayed. If not specified, the document base is used.

<ALT>
Optional. Text to be displayed for browsers that do not understand applets.

<NAME>
Optional. Assigns a name to an applet. Can be used by other applets for communication purposes.

<VSPACE>
Optional. Margin in pixels to be placed at the top and bottom of the applet.

<HSPACE>
Optional. Margin in pixels to be placed at the left and right of the applet.

<PARAM NAME="x...x" VALUE="x...x">
Optional. Used to pass parameters to the applet. The getParameter() method specifies the name and receives the value.

Applet Packaging

Applets should be packaged using jar files:

In order to get the features of Java 1.2, you must use the Java Plug-in:

Sample plug-in tag:

Original APPLET tag:



   <APPLET code="MessageApplet.class" width=350 height=125>
      <PARAM name="message" value=Hello World">
   </APPLET>


After conversion for Plug-in:


<OBJECT classid="clsid:8AD9C840-044E-11D1-BE39-00805F499D93"
  codebase=
  "http://java.sun.com/products/plugin/1.2/jinstall-12-win32.cab#Version=1,2,0,0"
      WIDTH=350 HEIGHT=125>
  <PARAM NAME=CODE VALUE="MessgeApplet.class">
  <PARAM NAME="type" VALUE="application/x-java-applet;version=1.2">
  <PARAM NAME="message" VALUE="Hello World">

   <COMMENT>
      <EMBED type="application/x-java-applet;verion=1.2">
        pluginspage=
          "http://java.sun.com/products/plugin/1.2/plugin-install.html"
          java_CODE="MessageApplet.class"
          WIDTH=350 HEIGHT=125 message="Hello World">
      </EMBED>
    </COMMENT>
</OBJECT>



Applet Security

In order to overcome the security restrictions, you must sign your applet:


AWT Programming

Drawing applications:

Form/data entry applications:

See the Components.java program for an example of all of the components listed.

Components should always be positioned using a LayoutManager and not absolute positioning.


Adding a Menu to an Application

Classes involved:

Adding a menu to an application:

  1. Create a menubar item:
    
    
       MenuBar mb = new MenuBar();
    
    
    
  2. Associate the menubar with frame:
    
      
      setMenuBar(mb);
    
    
    
  3. Create the dropdown menus:
    
    
       Menu menu1 = new Menu();
    
    
    
  4. Add the menu items to the menu and optionally define shortcut keys.
    
    
        MenuItem mi = new MenuItem("File");
        mi.setShortcut(new MenuShortcut(KeyEvent.VK_F2));
        menu1.add(mi);
    
    
    
  5. Add an action listener to each menu item.
    
    
        mi.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
    
             ...
           }
       });
    
    
    
  6. Add menu separators if needed:
    
    
       menu1.addSeparator();
    
    
    
  7. Repeat for all additional items.

  8. Optionally, define one of the menus to be the help menu. On some platforms, this positions this on the far right:
    
    
       mb.setHelpMenu(menu);
       
    
    

Creating a popup menu: