package clipborad;
import javax
import java
import java
import javax
import java
import javax
import javax
import javax
import java
/**
* Title: 用JAVA實現Undo
* Description:用JAVA實現Undo
* Copyright: Copyright (c)
* Company:
* @author: 彭建雄
* @version
*/
public class ClipTextArea extends JTextArea implements ClipboardOwner{
Clipboard strBoard = this
UndoManager undo = new UndoManager();
Document doc = getDocument();
public ClipTextArea() {
doc
public void undoableEditHappened(UndoableEditEvent e) {
doc_addedit(e);}
});
addActionMap();
}
public void addActionMap()
{
getActionMap()
public void actionPerformed(ActionEvent evt) {
try {
if (undo
undo
}
} catch (CannotUndoException e) {
}
}
});
getInputMap()
getActionMap()
public void actionPerformed(ActionEvent evt) {
try
{
if (undo
undo
}
} catch (CannotRedoException e) {
}
}
});
getInputMap()
getActionMap()
public void actionPerformed(ActionEvent evt) {
copy();
}
});
getInputMap()
getActionMap()
public void actionPerformed(ActionEvent evt) {
cut();
}
});
getInputMap()
getActionMap()
public void actionPerformed(ActionEvent evt) {
paste();
}
});
getInputMap()
}
public void copy()
{
String sCopy = getSelectedText();
if (sCopy != null)
{
StringSelection sSelection = new StringSelection(sCopy);
strBoard
}
}
public void cut()
{
String sCopy = getSelectedText();
if (sCopy != null)
{
StringSelection sSelection = new StringSelection(sCopy);
strBoard
this
}
}
public void paste()
{
Transferable sTransf = strBoard
if (sTransf!=null)
{
try
{
String sPaste = (String)sTransf
this
}
catch(UnsupportedFlavorException e)
{
}
catch(IOException ioe)
{
}
}
}
public void lostOwnership(Clipboard clipboard
{
}
void doc_addedit(UndoableEditEvent e)
{
undo
}
}
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/19462.html