2024年6月11日发(作者:)
JAVA中实现剪切,复制,粘贴功能
2008-05-12 15:50
要用到ansfer包中的Clipboard类
import .*;import .*;
import ansfer.*;
public class Test extends Frame implements ActionListener
{ MenuBar menubar; Menu menu;
MenuItem copy,cut,paste;
TextArea text1,text2;
Clipboard clipboard=null;
Test()
{ clipboard=getToolkit().getSystemClipboard();//获取系统剪
贴板。
menubar=new MenuBar();
menu=new Menu("Edit"); copy=new MenuItem("copy");
cut=new MenuItem ("cut"); paste=new MenuItem ("paste");
text1=new TextArea(20,20); text2=new TextArea(20,20);
ionListener(this); ionListener(this);
ionListener(this);
setLayout(new FlowLayout());
(menu);
(copy); (cut); (paste);
setMenuBar(menubar);
add(text1);add(text2);
setBounds(100,100,200,250); setVisible(true);pack();
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{(0);
}
}) ;
}
public void actionPerformed(ActionEvent e)
{ if(rce()==copy) //拷贝到剪贴板。
{ String temp=ectedText(); //拖动鼠标选取文本。
StringSelection text=new StringSelection(temp);
tents(text,null);
}
else if(rce()==cut) //剪贴到剪贴板。
{ String temp=ectedText(); //拖动鼠标选取文本。
StringSelection text=new StringSelection(temp);
tents(text,null);
int start=ectionStart();
int end =ectionEnd();
eRange("",start,end) ; //从Text1中删除被选取的文
本。
}
else if(rce()==paste) //从剪贴板粘贴数据。
{ Transferable contents=tents(this);
DataFlavor flavor= Flavor;
if( FlavorSupported(flavor))
try{ String str;
str=(String)nsferData(flavor);
(str);
}
catch(Exception ee){}
}
}
public static void main(String args[])
{ Test win=new Test();
}
}
发布评论