转自:http://bbs.chinaunix.net/forum/26/20030827/148099.html首先在Applet的init() 方法中建立Frame instance, 然后将Menus, Menubar都加上去即可。 (setMenuBar() 是 Frame下的方法)或者,找到Applet上层的Frame后依法炮制。 Container parent = getParent(); while (! (parent instanceof Frame) ) parent = parent.getParent(); Frame theFrame = (Frame) parent; 注意:第二个方法在Mac或某些browsers上并不可行。 如果您使用的是JDK 1.1,也可以考虑使用popup menu,这样就不必理会Frame的问题了。
其实是由于菜单不是从Component类派生出来的,不能用add方法在容器中添加菜单组件,而Menu、MenuBar和Frame都实现了setMenuBar方法,用它可以添加菜单条。
方法:
在init()中添加:
Frame myFrame = new Frame("myFrame Sample");//声明一个框架来包含菜单
myFrame.setSize(250,250);//设置Frame大小
myFrame.setMenuBar(menuBar1);//添加
myFrame.show();//显示
If you create your applet using javax.swing.JApplet, your applet won't run in most web browsers. At the time of this writing, Swing is not yet supported by the web browsers.