<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    有才華的人,別忘記給滋潤你的那塊土壤施肥

      BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
      28 隨筆 :: 5 文章 :: 147 評(píng)論 :: 0 Trackbacks
          在很多軟件中每個(gè)文本組件都有自定義的菜單,這個(gè)blogjava的編輯器就有這樣的菜單如:Cut , Copy,Paste,Delete,Select All,在Swing中若也想在JTextField,JTextArea,JEditorPane,JTextPane等等這些組件中都提供如此自定義菜單的功能,每個(gè)都寫繼承類?或者加鼠標(biāo)監(jiān)聽事件?但不管怎樣弄都會(huì)實(shí)現(xiàn)效果,只不過這樣動(dòng)靜很大,不好維護(hù),今天在網(wǎng)上看到一個(gè)很是方便的方法。

           大家都知道,Swing中所有的事件都是進(jìn)入java.awt.EventQueue這個(gè)隊(duì)列中等待,然后通過dispatchEvent方法進(jìn)行派發(fā),那么我們?cè)谶@里就寫個(gè)繼承EventQueue這個(gè)類,攔截所有的事件并對(duì)其進(jìn)行處理,我們的文本組件都是繼承與JTextComponent的,那么到這里我們就能為所有的文本組件定制一致的菜單了。效果如:
         



    見代碼:
    package org.kissjava.swingx.core;

    import java.awt.AWTEvent;
    import java.awt.Component;
    import java.awt.EventQueue;
    import java.awt.Point;
    import java.awt.Toolkit;
    import java.awt.datatransfer.DataFlavor;
    import java.awt.datatransfer.Transferable;
    import java.awt.event.ActionEvent;
    import java.awt.event.MouseEvent;

    import javax.swing.AbstractAction;
    import javax.swing.JPopupMenu;
    import javax.swing.MenuSelectionManager;
    import javax.swing.SwingUtilities;
    import javax.swing.text.JTextComponent;

    public class KJEventQueue extends EventQueue {
        @Override
         
    protected void dispatchEvent(AWTEvent event)
                
    super.dispatchEvent(event); 
         
                
    // interested only in mouseevents 
                if(!(event instanceof MouseEvent)) 
                    
    return
         
                MouseEvent me 
    = (MouseEvent)event; 
         
                
    // interested only in popuptriggers 
                if(!me.isPopupTrigger()) 
                    
    return
         
                
    // me.getComponent() retunrs the heavy weight component on which event occured 
                Component comp = SwingUtilities.getDeepestComponentAt(me.getComponent(), me.getX(), me.getY()); 
         
                
    // interested only in textcomponents 
                if(!(comp instanceof JTextComponent)) 
                    
    return
         
                
    // no popup shown by user code 
                if(MenuSelectionManager.defaultManager().getSelectedPath().length>0
                    
    return
         
                
    // create popup menu and show 
                JTextComponent tc = (JTextComponent)comp; 
                JPopupMenu menu 
    = new JPopupMenu(); 
                menu.add(
    new CutAction(tc)); 
                menu.add(
    new CopyAction(tc)); 
                menu.add(
    new PasteAction(tc)); 
                menu.add(
    new DeleteAction(tc)); 
                menu.addSeparator(); 
                menu.add(
    new SelectAllAction(tc)); 
                
                Point pt 
    = SwingUtilities.convertPoint(me.getComponent(), me.getPoint(), tc);
                menu.show(tc, pt.x, pt.y);
            }
     
         
    class CutAction extends AbstractAction
                JTextComponent comp; 
             
                
    public CutAction(JTextComponent comp)
                    
    super("Cut"); 
                    
    this.comp = comp; 
                }
     
             
                
    public void actionPerformed(ActionEvent e)
                    comp.cut(); 
                }
     
             
                
    public boolean isEnabled()
                    
    return comp.isEditable() 
                            
    && comp.isEnabled() 
                            
    && comp.getSelectedText()!=null
                }
     
            }
     
             
            
    // @author Santhosh Kumar T - santhosh@in.fiorano.com 
            class PasteAction extends AbstractAction
                JTextComponent comp; 
             
                
    public PasteAction(JTextComponent comp)
                    
    super("Paste"); 
                    
    this.comp = comp; 
                }
     
             
                
    public void actionPerformed(ActionEvent e)
                    comp.paste(); 
                }
     
             
                
    public boolean isEnabled()
                    
    if (comp.isEditable() && comp.isEnabled())
                        Transferable contents 
    = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(this); 
                        
    return contents.isDataFlavorSupported(DataFlavor.stringFlavor); 
                    }
    else 
                        
    return false
                }
     
            }
     
             
            
    // @author Santhosh Kumar T - santhosh@in.fiorano.com 
            class DeleteAction extends AbstractAction
                JTextComponent comp; 
             
                
    public DeleteAction(JTextComponent comp)
                    
    super("Delete"); 
                    
    this.comp = comp; 
                }
     
             
                
    public void actionPerformed(ActionEvent e)
                    comp.replaceSelection(
    null); 
                }
     
             
                
    public boolean isEnabled()
                    
    return comp.isEditable() 
                            
    && comp.isEnabled() 
                            
    && comp.getSelectedText()!=null
                }
     
            }
     
             
            
    // @author Santhosh Kumar T - santhosh@in.fiorano.com 
            class CopyAction extends AbstractAction
                JTextComponent comp; 
             
                
    public CopyAction(JTextComponent comp)
                    
    super("Copy"); 
                    
    this.comp = comp; 
                }
     
             
                
    public void actionPerformed(ActionEvent e)
                    comp.copy(); 
                }
     
             
                
    public boolean isEnabled()
                    
    return comp.isEnabled() 
                            
    && comp.getSelectedText()!=null
                }
     
            }
     
             
            
    // @author Santhosh Kumar T - santhosh@in.fiorano.com 
            class SelectAllAction extends AbstractAction
                JTextComponent comp; 
             
                
    public SelectAllAction(JTextComponent comp)
                    
    super("Select All"); 
                    
    this.comp = comp; 
                }
     
             
                
    public void actionPerformed(ActionEvent e)
                    comp.selectAll(); 
                }
     
             
                
    public boolean isEnabled()
                    
    return comp.isEnabled() 
                            
    && comp.getText().length()>0
                }
     
            }
     
    }


           到這里我們?nèi)绾问褂眠@個(gè)類呢?也很簡單,只要在主程序的入口處加上下面這句就行了:
    Toolkit.getDefaultToolkit().getSystemEventQueue().push(new KJEventQueue()); 

                也就是在main方法中調(diào)用,如:
     

    public static void main(String[] args) {
            Toolkit.getDefaultToolkit().getSystemEventQueue().push(
    new KJEventQueue()); 
            
    // TODO Auto-generated method stub
            SwingUtilities.invokeLater(new Runnable(){
                
    public void run(){
                    
    new KJEventQuequeDemo();
                }

            }
    );
        }



    資料來源:http://www.jroller.com/santhosh/entry/context_menu_for_textcomponents

    posted on 2009-06-27 23:31 kissjava 閱讀(1392) 評(píng)論(4)  編輯  收藏 所屬分類: swing

    評(píng)論

    # re: Swing中為文本組件定制統(tǒng)一的菜單 2009-06-28 21:58 rochoc
    不錯(cuò),學(xué)習(xí)了,謝謝  回復(fù)  更多評(píng)論
      

    # re: Swing中為文本組件定制統(tǒng)一的菜單 2009-06-28 23:50 zht
    Santhosh 是個(gè)人才  回復(fù)  更多評(píng)論
      

    # re: Swing中為文本組件定制統(tǒng)一的菜單 2009-06-29 00:03 枯寬
    @zht
    的確,最近沒事都在看他以前的文章  回復(fù)  更多評(píng)論
      

    # re: Swing中為文本組件定制統(tǒng)一的菜單 2009-07-01 13:26 找個(gè)美女做老婆
    Java高手群:Java樂園,群號(hào):28840096 Java樂園網(wǎng)站:http://www.javaly.cn 歡迎Java高手加入,大家一起交流經(jīng)驗(yàn),相互學(xué)習(xí),共同進(jìn)步  回復(fù)  更多評(píng)論
      

    主站蜘蛛池模板: 国产成人亚洲精品影院| 国产片免费福利片永久| 亚洲爆乳无码专区| 成人免费夜片在线观看| 无码专区一va亚洲v专区在线| 亚洲欧洲专线一区| 拔擦拔擦8x华人免费久久| 亚洲精品国产高清在线观看| 免费黄色一级毛片| 精品亚洲福利一区二区| 亚洲国产人成中文幕一级二级| 牛牛在线精品观看免费正| 国产精品亚洲综合一区| aaa毛片视频免费观看| 亚洲AV永久纯肉无码精品动漫| 永久在线观看免费视频 | 亚洲成A人片在线播放器| 成人午夜视频免费| 99亚洲男女激情在线观看| 最新精品亚洲成a人在线观看| a级毛片免费观看视频| 久久精品国产亚洲AV无码娇色| 国产男女爽爽爽爽爽免费视频| 亚洲最大中文字幕无码网站| 四虎永久在线精品免费观看地址 | 啊v在线免费观看| 国产免费MV大全视频网站| 亚洲国产香蕉碰碰人人| 国产成人A在线观看视频免费| 在线亚洲精品视频| 在线观看亚洲成人| 97碰公开在线观看免费视频| 色天使色婷婷在线影院亚洲| 亚洲精品无码久久久久去q | 91人成网站色www免费下载| 亚洲一区二区三区成人网站| 久久久久国产成人精品亚洲午夜 | 亚洲最大中文字幕| 亚洲国产成人久久一区久久| 国产在线一区二区综合免费视频| 亚洲成A人片在线播放器|