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

          歡迎光臨! 
          閑聊 QQ:1074961813

          隨筆分類(lèi)(357)

          我管理的群

          公共blog

          • n維空間
          • Email : java3d@126.com 群 : 12999758

          參與管理的論壇

          好友的blog

          我的其他blog

          朋友的網(wǎng)站

          搜索

          •  

          最新評(píng)論

          這個(gè)代碼來(lái)自本人的一個(gè)網(wǎng)友所寫(xiě),由于可能是初次接觸,寫(xiě)出來(lái)的代碼有些亂,本人做了分析,

          不足之處還請(qǐng)指出,謝謝支持!


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


          //***********************************************************************************/
          //????????????????????????????????? con1函數(shù)類(lèi)????????????????????????????????????? /
          //***********************************************************************************/

          public class con1 extends JPanel{
          ??
          ?
          ??
          ?? private JPopupMenu popup =new JPopupMenu();??????? //創(chuàng)建JPopupMenu對(duì)象,菜單popup

          ?? public con1(){??????????????????????????????????? //該函數(shù)用于接收???????????

          ? ActionListener menulistener =new ActionListener()???????? //創(chuàng)建事件監(jiān)聽(tīng)
          ?? {

          ???????????? public void actionPerformed(ActionEvent e){????????????? //發(fā)生操作時(shí)調(diào)用?????
          ??????????????????????????
          ?????????????????????????? System.out.println("popup menu item [? "+e.getActionCommand()+" ]被單擊");? //打印接收的命令
          ??????????????????????????????????????????
          ???????????????????????????????????????????? }
          ?? };

          String[] files={"查找","打開(kāi)新文件","發(fā)送郵件","系統(tǒng)設(shè)置","退出"};??????????? //創(chuàng)建數(shù)組
          JMenuItem item;???????????????????????????????????????????????????????????
          ????????????????????????????????????????????????????????????????????
          for(int i=0;i<files.length;i++){??????????????????????????????????????????????? //這里files.length=5,即循環(huán)5次
          ????????????? popup.add(item =new JMenuItem(files[i]));???????????????????????? //將JMenuItem加入popup菜單
          ????????????? item.setHorizontalTextPosition(item.RIGHT);?????????????????????? //設(shè)置文字顯示位置,居右
          ????????????? item.addActionListener(menulistener);???????????????????????????? //讓item和menulistener對(duì)應(yīng)起來(lái)
          ????????????????????????????? }??????????????????????????????????????????????????????????????????????????????????
          popup.addSeparator();?
          popup.setPopupSize(150,150);??????????????????????????????????????????????????????????????????????????? //彈出與菜單有關(guān)的分隔符
          popup.setBorder(new BevelBorder(BevelBorder.RAISED));?????????????????????????? //設(shè)置邊框樣式
          popup.addPopupMenuListener( new printlistener());?????????????????????????? //導(dǎo)入菜單監(jiān)聽(tīng)

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

          con1.this.addMouseListener(new mouselistener());?????????????????????????????????????? //導(dǎo)入鼠標(biāo)監(jiān)聽(tīng)

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

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

          ?


          //***********************************************************************************/
          //??????????????? 這兩個(gè)函數(shù)將被后面的con1函數(shù)類(lèi)調(diào)用?????????????????????????????? /
          //***********************************************************************************/
          class printlistener implements PopupMenuListener{???????? //創(chuàng)建監(jiān)聽(tīng)菜單
          ?
          ??????????????????? public void popupMenuCanceled(PopupMenuEvent e){
          ???????????????????????????????????????????????????? System.out.println("彈出菜單被隱藏:");
          ??????????????????????????????????????????????????????????????????? }
          ??????????????????? public void popupMenuWillBecomeInvisible(PopupMenuEvent e){
          ???????????????????????????????????????????????????? System.out.println("彈出菜單不可見(jiàn):");
          ??????????????????????????????????????????????????????????????????? }
          ??????????????????? public void popupMenuWillBecomeVisible(PopupMenuEvent e){
          ?????????????????????????????????????????????????? System.out.println("彈出菜單被激活:");
          ??????????????????????????????????????????????????????????????????? }
          }


          class mouselistener extends MouseAdapter???????????????????????????? //鼠標(biāo)監(jiān)聽(tīng)函數(shù)
          {????????????????????????????????????????????????????????????????? //捕獲鼠標(biāo)各種行為
          ??????? public void mouseClicked(MouseEvent e){?
          ???????????????????????????????????????????????? check(e);}??????????? //單擊
          ??????? public void mouseEntered(MouseEvent e){ check(e);}??????????? //鼠標(biāo)進(jìn)入到組件(進(jìn)入菜單)
          ??????? public void mouseExited(MouseEvent e){ check(e);}????????????? //鼠標(biāo)離開(kāi)組件
          ??????? public void mousePressed(MouseEvent e){ check(e);}???????????? // 鼠標(biāo)在組件上按下
          ??????? public void mouseReleased(MouseEvent e){ check(e);}??????????? //鼠標(biāo)按鈕在組件上釋放
          ??????? private void check(MouseEvent e){
          ?????????????????????????? if(e.isPopupTrigger()){
          ??????????????????????????????????????? popup.show(con1.this,e.getX(),e.getY()); //在指定位置顯示右鍵彈出菜單
          ??????????????????????????????
          ???????????????????????????????????????????????? }
          ???????????????????????????????????????? }
          ???
          }


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

          ?

          //***********************************************************************************/
          //???????????????????????????????? 主函數(shù)??????????????????????????????????????????? /
          //***********************************************************************************/


          public static void main(String[] args){
          ?????????????????????
          ??????????????????
          ???????????????????? JFrame frame =new JFrame("菜單演示");?????????????????????? //創(chuàng)建JFrame名字菜單演示
          ????????????????????
          ??????????????????????? frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);?????? //返回用戶在此窗體上發(fā)起 "close" 時(shí)
          ??????????????????????????????????????????????????????????????????????????????????? //執(zhí)行的操作, 關(guān)閉 JFrame???????????????????????

          ????????????????????????????????? frame.setContentPane(new con1());?????????????????????????? //在窗體上添加面板類(lèi)con1(菜單)
          ??????????????????????????????
          ??????????????????????? frame.setSize(300,300);????????????????????????????????????? //窗口大小300*300
          ??????????????????????????????
          ??????????????????????? frame.setVisible(true);????????????????????????????????????? //激活控件
          ????????????????????????????
          ?????????????????????????????????????? }

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



          地震讓大伙知道:居安思危,才是生存之道。
          posted on 2007-02-25 09:39 小尋 閱讀(694) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): j2se/j2ee/j2me
          主站蜘蛛池模板: 会昌县| 马关县| 桃园县| 响水县| 沾化县| 洛南县| 新竹市| 岗巴县| 偃师市| 类乌齐县| 修水县| 定安县| 福海县| 十堰市| 开江县| 东城区| 读书| 平江县| 天峻县| 铜川市| 毕节市| 越西县| 汝州市| 望奎县| 新营市| 云浮市| 绥棱县| 石泉县| 浠水县| 麻阳| 新源县| 伊川县| 白河县| 昆山市| 六枝特区| 新巴尔虎右旗| 都安| 长岭县| 恩施市| 忻城县| 开远市|