import java.awt.*;
import java.awt.event.*;
public class MenuTest
{
public static void main(String[]args)
{
MenuFrame form = new MenuFrame();//创建MenuFrame的对象form,生成Frame和菜单
form.setVisible(true);
}
}
class MenuTest extends Frame implements ActionListener //继承Frame类
{
Label lb=new Label("here come command");
MenuBar m_MenuFile=new MenuBar();
Menu menuFile=new MenuItem("File");
MenuItem f1=new MenuItem("Open");
MenuItem f2=new MenuItem("Close");
MenuItem f3=new MenuItem("Exit");
Menu menuEdit=new Menu("Edit");
MenuItem e1=new MenuItem("Copy");
MenuItem e2=new MenuItem("Cut");
MenuItem e3=new MenuItem("Paste");
MenuFrame()
{
super("Menu Frame"); //设置Frame标题
setLayout(new FlowLayout());
m_MenuBar.add(menuFile);
menuFile.add(f1);
f1.addActionListener(this);
menuFile.add(f2);
f2.addActionListener(this);
menuFile.add(f3);
f3.addActionListener(this);
m_MenuBar.add(menuEdit);
menuFile.add(e1);
e1.addActionListener(this);
menuFile.add(e2);
e2.addActionListener(this);
menuFile.add(e3);
e3.addActionListener(this);
setMenuBar(m_MenuBar);
add(lb);
setSize(300,300);
addWindowListener(new Tuichu());
}
public void actionPerformed(ActionEvent e)
{
lb.setText(e.getActionCommand);
}
}
class Tuichu extends WindowAdapter //使用window的事件适配器推出Frame
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}