import java.awt.*;
import java.awt.event.*;
public class MenuTest
{
Frame f = new Frame("MenuTest");
Label l = new Label("这里是状态栏");
Label l1 = new Label("文件",Label.CENTER);
Label l2 = new Label("退出",Label.CENTER);
Panel pc = new Panel();
CardLayout c = new CardLayout();
MenuBar mb = new MenuBar();
Menu menu1 = new Menu("文件");
Menu menu2 = new Menu("编辑");
MenuItem menuitem1 = new MenuItem("打开");
MenuItem menuitem2 = new MenuItem("退出");
MenuItem menuitem3 = new MenuItem("剪切");
MenuItem menuitem4 = new MenuItem("粘贴");
CheckboxMenuItem chmenuitem = new CheckboxMenuItem("状态栏");
public static void main(String[] args)
{
MenuTest that = new MenuTest();
that.go();
}
void go()
{
f.setSize(500,400);
f.setMenuBar(mb);
f.setLayout(new BorderLayout());
mb.add(menu1);
mb.add(menu2);
menu1.add(menuitem1);
menu1.add(menuitem2);
menu1.addSeparator();
menu1.add(chmenuitem);
menu2.add(menuitem3);
menu2.add(menuitem4);
f.add("Center",pc);
pc.add(l1,"wenjian");
pc.add(l2,"tuichu");
f.add("South",l);
f.setVisible(true);
menuitem1.addActionListener(new MenuHandler(1));
menuitem2.addActionListener(new MenuHandler(2));
chmenuitem.addItemListener(new MenuDsp());
f.addWindowListener(new WindowHandler());
}
class MenuDsp implements ItemListener
{
public void itemStateChanged(ItemEvent e)
{
if (chmenuitem.getState())
{
l.setVisible(true);
}
else
l.setVisible(false);
}
}
class MenuHandler implements ActionListener
{
private int ch;
MenuHandler(int select)
{
ch = select;
}
public void actionPerformed(ActionEvent e)
{
switch(ch)
{
case 1:c.show(pc,"wenjian"); break;
case 2:c.show(pc,"tuichu"); break;
}
l.setText(e.getActionCommand());
}
}
class WindowHandler extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(1);
}
}
}