随笔 - 303  文章 - 883  trackbacks - 0
<2007年2月>
28293031123
45678910
11121314151617
18192021222324
25262728123
45678910

欢迎光临! 
闲聊 QQ:1074961813

随笔分类(357)

我管理的群

公共blog

  • n维空间
  • Email : java3d@126.com 群 : 12999758

参与管理的论坛

好友的blog

我的其他blog

朋友的网站

搜索

  •  

最新评论

这个代码来自本人的一个网友所写,由于可能是初次接触,写出来的代码有些乱,本人做了分析,

不足之处还请指出,谢谢支持!


//package gui.con5;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;


//***********************************************************************************/
//                                  con1函数类                                      /
//***********************************************************************************/

public class con1 extends JPanel{
  
 
  
   private JPopupMenu popup =new JPopupMenu();        //创建JPopupMenu对象,菜单popup

   public con1(){                                    //该函数用于接收           

  ActionListener menulistener =new ActionListener()         //创建事件监听
   {

             public void actionPerformed(ActionEvent e){              //发生操作时调用     
                          
                           System.out.println("popup menu item [  "+e.getActionCommand()+" ]被单击");  //打印接收的命令
                                          
                                             }
   };

String[] files={"查找","打开新文件","发送邮件","系统设置","退出"};            //创建数组
JMenuItem item;                                                           
                                                                    
for(int i=0;i<files.length;i++){                                                //这里files.length=5,即循环5次
              popup.add(item =new JMenuItem(files[i]));                         //将JMenuItem加入popup菜单
              item.setHorizontalTextPosition(item.RIGHT);                       //设置文字显示位置,居右
              item.addActionListener(menulistener);                             //让item和menulistener对应起来
                              }                                                                                  
popup.addSeparator(); 
popup.setPopupSize(150,150);                                                                            //弹出与菜单有关的分隔符
popup.setBorder(new BevelBorder(BevelBorder.RAISED));                           //设置边框样式
popup.addPopupMenuListener( new printlistener());                           //导入菜单监听

//******************************************************本人加入部分

con1.this.addMouseListener(new mouselistener());                                       //导入鼠标监听

//******************************************************
}

//***********************************************************************************/

 


//***********************************************************************************/
//                这两个函数将被后面的con1函数类调用                               /
//***********************************************************************************/
class printlistener implements PopupMenuListener{         //创建监听菜单
 
                    public void popupMenuCanceled(PopupMenuEvent e){
                                                     System.out.println("弹出菜单被隐藏:");
                                                                    }
                    public void popupMenuWillBecomeInvisible(PopupMenuEvent e){
                                                     System.out.println("弹出菜单不可见:");
                                                                    }
                    public void popupMenuWillBecomeVisible(PopupMenuEvent e){
                                                   System.out.println("弹出菜单被激活:");
                                                                    }
}


class mouselistener extends MouseAdapter                             //鼠标监听函数
{                                                                  //捕获鼠标各种行为
        public void mouseClicked(MouseEvent e){ 
                                                 check(e);}            //单击
        public void mouseEntered(MouseEvent e){ check(e);}            //鼠标进入到组件(进入菜单)
        public void mouseExited(MouseEvent e){ check(e);}              //鼠标离开组件
        public void mousePressed(MouseEvent e){ check(e);}             // 鼠标在组件上按下
        public void mouseReleased(MouseEvent e){ check(e);}            //鼠标按钮在组件上释放
        private void check(MouseEvent e){
                           if(e.isPopupTrigger()){
                                        popup.show(con1.this,e.getX(),e.getY()); //在指定位置显示右键弹出菜单
                              
                                                 }
                                         }
   
}


//***********************************************************************************/

 

//***********************************************************************************/
//                                 主函数                                            /
//***********************************************************************************/


public static void main(String[] args){
                     
                  
                     JFrame frame =new JFrame("菜单演示");                       //创建JFrame名字菜单演示
                    
                        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       //返回用户在此窗体上发起 "close" 时
                                                                                    //执行的操作, 关闭 JFrame                       

                                  frame.setContentPane(new con1());                           //在窗体上添加面板类con1(菜单)
                              
                        frame.setSize(300,300);                                      //窗口大小300*300
                              
                        frame.setVisible(true);                                      //激活控件
                            
                                       }

//***********************************************************************************/



地震让大伙知道:居安思危,才是生存之道。
posted on 2007-02-25 09:39 小寻 阅读(686) 评论(0)  编辑  收藏 所属分类: j2se/j2ee/j2me

只有注册用户登录后才能发表评论。


网站导航: