Computer Science Canada

Help with menus not working when applet is show in HTML

Author:  Integrate [ Sat Jun 07, 2008 2:07 pm ]
Post subject:  Help with menus not working when applet is show in HTML

I have copied a piece of code that creates a menu:

code:

import java.applet.*;
import java.awt.*;
import java.lang.*;
import java.util.*;

public class MenubarApplet extends Applet {
  private Frame    frame;
  private MenuBar  mb;
  private Menu     fm, hm;
  private MenuItem ol, of, pr, hi, hc;
  private Label    message =
    new Label("Select an option from a menu (nothing will happen)");

  public void init() {

    setLayout(new BorderLayout());
    add("Center",message);

    Object f = getParent ();
    while (! (f instanceof Frame))
      f = ((Component) f).getParent ();
    frame = (Frame) f;

    mb = new MenuBar();

    mb.add(fm = new Menu("File"));
    mb.add(hm = new Menu("Help"));
    mb.setHelpMenu(hm);

    fm.add(ol = new MenuItem("Open Location"));
    fm.add(of = new MenuItem("Open File"));
    fm.addSeparator();
    fm.add(pr = new MenuItem("Print"));

    hm.add(hi = new MenuItem("Index"));
    hm.add(hc = new MenuItem("Content"));

    frame.setMenuBar(mb);
    frame.pack();
  }

  public boolean action(Event e, Object arg) {
    if (e.target == ol || e.target == of || e.target == pr ||
        e.target == hi || e.target == hc) {
      message.setText((String) arg);
      return true;   
    }
    return super.action(e,arg);
  }
}


It works fine in my compiler, but when I open the HTML file that takes the .class, it doesn't show the menu. Does anyone know how to fix this?

EDIT:
heres my HTML code:

code:

<html>
<head>
<title>Title</title>
<body bgcolor=white>
<applet code=MenubarApplet.class width="800" height="500">
Your browser does not support the applet tag.
</applet>
</body>


: