zoukankan      html  css  js  c++  java
  • [转载]JAVA实现鼠标右键功能

    原文地址:JAVA实现鼠标右键功能作者:beexk
    如何实现右键功能呢?(PopupMenu)
    有两个步骤:
    1、谁来实现,也就是该右键的source是什么。
                  (直接把PopupMenu加到对象source上即可)
    2、添加鼠标按钮
    source.addMouseListener(new MouseAdapter(){
        public void mousePressed(MouseEvent me)                       
            int mode=me.getModifiers();
            if(mode&InputEvent.BUTTON3_MASK)
             {
               popuMenu.show(source,me.getX(),me.getY());
             }});
    ------------------------------------------------------------
    附上可以实现该功能的代码:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class TreeTest extends JPanel
    {
     PopupMenu pm=new PopupMenu();
     MenuItem it1=new MenuItem("new");
     MenuItem it2=new MenuItem("open");
     TreeTest()
     {
      pm.add(it1);
      pm.add(it2);
      add(pm);
       addMouseListener(new MouseAdapter()
      {
       public void mousePressed(MouseEvent e)
       {
        mouE(e);
       }
      });
     
     }
     void mouE(MouseEvent e)
     {
      int mod=e.getModifiers();
      System.out.println("mod="+mod);
      if(mod==InputEvent.BUTTON3_MASK)
      {
       pm.show(this, e.getX(), e.getY());
      }
     }
     public static void main(String[] args)
     {
      TreeTest tt=new TreeTest();
      JFrame jf=new JFrame("df");
      jf.getContentPane().add(tt);
      jf.setSize(600,400);
      jf.setVisible(true);
      
     }
    }
  • 相关阅读:
    为什么Android的图片质量会比iPhone的差?
    第二次作业-编程
    印像最深的三位老师
    c++的类型转换(转)
    SpringMVC入门详解
    java基础
    springmvc
    Maven配置下载包速度更快
    spring中的bean.xml
    pom.xml依赖
  • 原文地址:https://www.cnblogs.com/liuzhuqing/p/7481073.html
Copyright © 2011-2022 走看看