//做菜单
import java.awt.*;
import java.awt.event.*;
class MyMenuDemo
{
private Frame f;
private MenuBar mb;
private Menu m,subMenu;
private MenuItem closeItem,subItem;
MyMenuDemo()
{
init();
}
public void init()
{
f=new Frame("我的菜单测试");
f.setBounds(300,200,600,500);
f.setLayout(new FlowLayout());
mb=new MenuBar();//只是一个Bar 参数为空
m=new Menu("文件");
subMenu=new Menu("子菜单");
subItem=new MenuItem("子条目");
closeItem=new MenuItem("退出");
f.setMenuBar(mb);
mb.add(m);
m.add(subMenu);
subMenu.add(subItem);
m.add(closeItem);
myEvent();
f.setVisible(true);
}
public void myEvent()
{
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
closeItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
);
}
public static void main(String[] args)
{
new MyMenuDemo();
}
}