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

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

                 大家都知道,Swing中所有的事件都是進入java.awt.EventQueue這個隊列中等待,然后通過dispatchEvent方法進行派發(fā),那么我們在這里就寫個繼承EventQueue這個類,攔截所有的事件并對其進行處理,我們的文本組件都是繼承與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)绾问褂眠@個類呢?也很簡單,只要在主程序的入口處加上下面這句就行了:
          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 閱讀(1384) 評論(4)  編輯  收藏 所屬分類: swing

          評論

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

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

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

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

          主站蜘蛛池模板: 高青县| 靖宇县| 北票市| 怀集县| 嘉鱼县| 祁东县| 厦门市| 陆河县| 安溪县| 达孜县| 靖江市| 博湖县| 汉川市| 叙永县| 读书| 元氏县| 顺平县| 延庆县| 伊金霍洛旗| 漯河市| 崇仁县| 盐城市| 舒兰市| 临汾市| 湟中县| 兴文县| 德化县| 历史| 雷山县| 湖南省| 白河县| 重庆市| 仁怀市| 大宁县| 平南县| 建水县| 松溪县| 宁远县| 合川市| 苍南县| 肥西县|