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编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |