|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
你说是sun公司对她研究的透还是微软?针对自己工具开发的.net网页编程性能上肯定会站上风的。
在良多软件中每一个文本组件都有自界说的菜单,这个blogjava的编纂器就有如许的菜单如:Cut,Copy,Paste,Delete,SelectAll,在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(newKJEventQueue());
也就是在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();
}
});
}
唉!都是钱闹的1.Swing和.net网页编程开发比较------从市场份额看.net网页编程开发主要占据大部分的中小型和中型的的桌面开发,原因是它封装了很多工具 |
|