要用到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();
}
}
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();
}
}
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.
textArea.paste();
textArea.copy();
textArea.cut();
都能搞定classjava写的很规范
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的方法,但思想都是一样的.
textArea.copy();
textArea.cut();