弹出式菜单 java编程思想中例程的看法

源程序如下: 

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javawork.swing.*;
public class Popup extends JApplet
{
 private JPopupMenu popup=new JPopupMenu();
 private JTextField t=new JTextField(10);
 
 public void init()
 {
  Container cp=getContentPane();
  cp.setLayout(new FlowLayout());
  cp.add(t);
  ActionListener al =new ActionListener ()
  {
   public void actionPerformed(ActionEvent e)
   {
    t.setText(((JMenuItem)e.getSource()).getText());
   }
  };
  JMenuItem m =new JMenuItem("Hither");
  m.addActionListener(al);
  popup.add(m);
  m=new JMenuItem("yon");
  m.addActionListener(al);
  popup.add(m);
  m=new JMenuItem("Afar");
  m.addActionListener(al);
  popup.add(m);
  //void addSeparator()
          //Appends a new separator at the end of the menu.
  popup.addSeparator();
  m= new JMenuItem("Stay here");
  m.addActionListener(al);
  popup.add(m);
  PopupListener pl =new PopupListener();
  addMouseListener(pl);
  t.addMouseListener(pl);///
 }
 class PopupListener extends MouseAdapter
 {
  public void mousePressed(MouseEvent e)
  {
        maybeShowPopup(e);
   }
   public void mouseReleased(MouseEvent e)
   {
        maybeShowPopup(e);
   }
   private void maybeShowPopup(MouseEvent e)
   {
    if(e.isPopupTrigger())
      popup.show(((JApplet)e.getComponent()).getContentPane(),e.getX(),e.getY());
   }
 }
 public static void main(String [] args)
 {
  Console.run(new Popup(),300,200);
 }
}

观点 :

 t.addMouseListener(pl);///

这一行大无必要,添加这一行之后  在文本域 右键单击  就会在控制台看到好多异常的出现,

 

如果去掉那一行的 话    就没有这些异常的发生了  不过再文本域单击的时候  就不会出现弹出式菜单的效果!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值