這個(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);????????????????????????????????????? //激活控件
????????????????????????????
?????????????????????????????????????? }
//***********************************************************************************/
地震讓大伙知道:居安思危,才是生存之道。
