如何在JAVA中实现撤销,剪切,复制,粘贴4大功能
小弟初学JAVA,写一个很简单的Notepad。
请问各路高手,如何用代码实现将选中的文本进行上述的4大功能操作?
怎样才能截获鼠标所选中的文本?
问题点数:100、回复次数:19Top
1 楼cnidb(老菜)回复于 2005-01-01 16:33:27 得分 0
帮顶.Top
2 楼classjava(原始野人)回复于 2005-01-01 16:41:21 得分 30
要用到java.awt.datatransfer包中的Clipboard类
import java.awt.*;import java.awt.event.*;
import java.awt.datatransfer.*;
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);
copy.addActionListener(this); cut.addActionListener(this);
paste.addActionListener(this);
setLayout(new FlowLayout());
menubar.add(menu);
menu.add(copy); menu.add(cut); menu.add(paste);
setMenuBar(menubar);
add(text1);add(text2);
setBounds(100,100,200,250); setVisible(true);pack();
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit(0);
}
}) ;
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==copy) //拷贝到剪贴板。
{ String temp=text1.getSelectedText(); //拖动鼠标选取文本。
StringSelection text=new StringSelection(temp);
clipboard.setContents(text,null);
}
else if(e.getSource()==cut) //剪贴到剪贴板。
{ String temp=text1.getSelectedText(); //拖动鼠标选取文本。
StringSelection text=new StringSelection(temp);
clipboard.setContents(text,null);
int start=text1.getSelectionStart();
int end =text1.getSelectionEnd();
text1.replaceRange("",start,end) ; //从Text1中删除被选取的文本。
}
else if(e.getSource()==paste) //从剪贴板粘贴数据。
{ Transferable contents=clipboard.getContents(this);
DataFlavor flavor= DataFlavor.stringFlavor;
if( contents.isDataFlavorSupported(flavor))
try{ String str;
str=(String)contents.getTransferData(flavor);
text2.append(str);
}
catch(Exception ee){}
}
}
public static void main(String args[])
{ Test win=new Test();
}
}
Top
3 楼yanudd((明明)(http://www.comejava.com)(中国java联盟))回复于 2005-01-01 16:44:01 得分 0
不错,学习,哈哈Top
4 楼jgsfy(阿庙)回复于 2005-01-01 16:46:39 得分 0
学习
upTop
5 楼yuheng_gdut(1dkou)回复于 2005-01-01 17:14:54 得分 30
小弟我也刚学JAVA不久,好像复制粘贴剪切只需要textArea.paste();textArea.copy();textArea.cut();就搞定了。Top
6 楼yuheng_gdut(1dkou)回复于 2005-01-01 17:16:51 得分 0
接楼上 文档里有写:
public void copy()
Transfers the currently selected range in the associated text model to the system clipboard, leaving the contents in the text model. The current selection remains intact. Does nothing for null selections.Top
7 楼fjh1119(阿修罗)回复于 2005-01-01 17:28:38 得分 0
小弟在测试:
textArea.paste();
textArea.copy();
textArea.cut();
都能搞定
classjava写的很规范Top
8 楼nwpulipeng(☆→【★海阔天空★】)回复于 2005-01-01 19:06:43 得分 0
以前看到过notepad的例子代码,我找找。。。Top
9 楼fjh1119(阿修罗)回复于 2005-01-01 20:33:13 得分 0
在写撤销的时候,怎么样最好?Top
10 楼sxj0608(不过如此)回复于 2005-01-01 22:51:45 得分 0
学习一下 :redo undoTop
11 楼cq_pro(重庆制造)回复于 2005-01-02 08:57:02 得分 0
有个专门的类就是undo和redo的,谁能讲解一下啊!~Top
12 楼xjyngl(hong)回复于 2005-01-02 09:12:54 得分 20
JTextField提供了undo/redo的方法,你只要在你写的类中首先定义一个UNDOMANAGER .UndoManager mg = new UndoManager();然后继承UndoableEditListener.,在实现的方法中 public void undoableEditHappened(UndoableEditEvent parm1) {
mg.addEdit(parm1.getEdit());//将EDIT注册到UNDOMANAGER中
}然后给JTextField添加jTextArea1.getDocument().addUndoableEditListener(this);.增加两个BUTTON,一个实现UNDO,一个实现REDO.UDNO方法: public void undo() {
mg.undo();
}
REDO方法
public void redo() {
mg.redo();
}
试试.很管用.如果不是要实现JTEXTFIELD的UNDOREDO可能就需要自己写UNDO/REDO的方法,但思想都是一样的.
Top
13 楼wadsunglow(东)回复于 2005-01-02 09:51:45 得分 0
textArea.paste();
textArea.copy();
textArea.cut();
Top
14 楼wadsunglow(东)回复于 2005-01-02 09:59:19 得分 20
如果需要的话我给你发个比较完整点的记事本例子
QQ:99403260Top
15 楼superman421(38度的雪)回复于 2005-01-02 10:13:23 得分 0
markTop
16 楼song3721(宋宋)回复于 2005-01-02 10:33:20 得分 0
studying...Top
17 楼zzzle(Vincent)回复于 2005-01-02 11:37:23 得分 0
快捷键是同样的,Ctrl+c,Ctrl+v,Ctrl+x,Ctrl+z,不写任何代码也能实现上述功能Top
18 楼bbmyth()回复于 2005-01-02 12:02:33 得分 0
受用了。谢谢Top
19 楼km3(北落师门)回复于 2005-01-02 15:25:53 得分 0
markTop




