/*ch0713.java*/ 
import java.awt.*;
import java.awt.event.*;
import javax.swing.ImageIcon;
import javax.swing.*;
public class ch0713 extends JFrame implements ActionListener,MouseListener
{ ImageIcon icon=new ImageIcon("ms.jpg");
  JTextArea msg=new JTextArea();
  JPopupMenu pm=new JPopupMenu();
  JMenuItem item1=new JMenuItem("hello");
  JMenuItem item2=new JMenuItem("我是弹出式菜单");
  JMenuItem item3=new JMenuItem("你会用了吗？");
  ch0713()
  { setIconImage(icon.getImage());
    setTitle("弹出式菜单实例");
    setSize(350,200);
    Container c=getContentPane();
    addWindowListener(new WindowAdapter()
    { public void windowClosing(WindowEvent e)
      { System.exit(0);
      }
    });
    c.add(msg);
    msg.add(pm);        //将弹出式菜单加入到文本区中
    pm.add(item1);
    pm.add(item2);
    pm.add(item3);
    item1.addActionListener(this);
    item2.addActionListener(this);
    item3.addActionListener(this);
    msg.addMouseListener(this);
    show();
  }
  public void actionPerformed(ActionEvent e)
  { msg.append("你选择了 “"+e.getActionCommand()+"”\n");
  }
  public void mouseReleased(MouseEvent e)
  { if(e.isPopupTrigger())       //判断是否单击鼠标右键
      //在当前窗口鼠标右键点击的位置显示弹出式菜单
    pm.show(this,e.getX(),e.getY());
  }
  public void mouseClicked(MouseEvent e){ }
  public void mouseEntered(MouseEvent e){ }
  public void mouseExited(MouseEvent e){ }
  public void mousePressed(MouseEvent e){ }
  public static void main(String ages[ ])
  { new ch0713();
  }
}

