/*ch0712.java*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.ImageIcon;
import javax.swing.*;
public class ch0712 extends JFrame implements ActionListener,ItemListener
{ ImageIcon icon=new ImageIcon("ms.jpg");
  JTextField msg=new JTextField();
  JMenuBar mb=new JMenuBar();
  JMenu m1=new JMenu("File");
  JMenu m2=new JMenu("二级菜单");
  JMenuItem item=new JMenuItem("普通菜单项");
  JCheckBoxMenuItem checkbox=new JCheckBoxMenuItem("复选菜单项");
  JMenuItem exit=new JMenuItem("退出");
  public ch0712()
  { setIconImage(icon.getImage());
    addWindowListener(new WindowAdapter()
    { public void windowClosing(WindowEvent e)
      { System.exit(0);
      }
    });
    Container c=getContentPane();
    setTitle("菜单综合应用实例");
    setSize(350,200);
    c.add(msg);
    mb.add(m1);
    m1.add(m2);
    checkbox.setState(true);
    m1.add(item);
    m1.setMnemonic('F');   //用此方法来设置JMenu和JMenuItem的快捷键
    item.setAccelerator(KeyStroke.getKeyStroke('I',java.awt.Event.CTRL_MASK,false));
    m1.add(checkbox);
    m1.addSeparator();
    m1.add(exit);
    m2.add("菜单项A");
    m2.add("菜单项B");
    item.addActionListener(this);
    checkbox.addItemListener(this);
    exit.addActionListener(this);
    setJMenuBar(mb);
    show();
  }
  public void actionPerformed(ActionEvent e)
  { if(e.getSource()==exit)
      System.exit(0);
    else
      msg.setText(e.getActionCommand()+"被打开");
  }
  public void itemStateChanged(ItemEvent e)
  { if(e.getSource()==checkbox)
      if(checkbox.getState())
        msg.setText(checkbox.getText()+"被选中");
      else
        msg.setText(checkbox.getText()+"被取消");
  }
  public static void main(String arg[])
  { new ch0712();
  }
}
