当前位置:七道奇文章资讯编程技术Java编程
日期:2011-03-22 16:13:00  来源:本站整理

Swing中为文本组件定制统一的菜单[Java编程]

赞助商链接



  本文“Swing中为文本组件定制统一的菜单[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:

在很多软件中每个文本组件都有自定义的菜单,这个blogjava的编辑器就有这样的菜单如:Cut , Copy,Paste,Delete,Select All,在Swing中若也想在JTextField,JTextArea,JEditorPane,JTextPane等等这些组件中都供应如此自定义菜单的功效,每个都写担当类?大概加鼠标监听事件?但不管怎样弄城市实现效果,只不过这样动静很大,不好保护,本日在网上看到一个非常便利的办法.

大家都知道,Swing中全部的事件都是进入java.awt.EventQueue这个行列中等候,然后通过dispatchEvent办法举行派发,那么我们在这里就写个担当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;
             }
         }
}

到这里我们若何利用这个类呢?也很简单,只要在主程序的进口处加上下面这句就行了:

Toolkit.getDefaultToolkit().getSystemEventQueue().push(new KJEventQueue());

也就是在main办法中调用,如:

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();
             }
         });
     }


  以上是“Swing中为文本组件定制统一的菜单[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
  • Swing更多的知识
  • java swing菜单例子
  • java Swing列表框和组合框
  • java Swing入门
  • Swing有哪些所长
  • Swing显示框架
  • 在NetBeans 4.1中操纵Swing组件
  • 操作Java Swing筹划通用对话框
  • 操作Java Swing筹划尺度对话框
  • 用Swing编写矫捷的图形用户界面
  • <b>Swing的实用工具:Swing Utilities</b>
  • 深化浅出Java多线程(2)-Swing中的EDT(事件分发线程)
  • 本文地址: 与您的QQ/BBS好友分享!
    • 好的评价 如果您觉得此文章好,就请您
        0%(0)
    • 差的评价 如果您觉得此文章差,就请您
        0%(0)

    文章评论评论内容只代表网友观点,与本站立场无关!

       评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
    Copyright © 2020-2022 www.xiamiku.com. All Rights Reserved .