用JAVA實現Undo、Redo,Copy、Paste、Cut (轉)
package clipborad;
import x..JTextArea;
import java.awt.datatraner.*;
import java.awt.*;
import javax.swing.*;
import java.io.*;
import javax.swing.undo.*;
import javax.swing.text.*;
import javax.swing.event.*;
import java.awt.event.*;
/**
* Title: 用JAVA實現Undo、Redo,Copy、Paste、Cut
* Description:用JAVA實現Undo、Redo,Copy、Paste、Cut
* Copyright: Copyright (c) 2001
* Company:
* @author: 彭建雄
* @version 1.0
*/
public class ClipTextArea extends JTextArea implements ClipboardOwner{
Clipboard strBoard = this.getToolkit().getSystemClipboard();
Unanager undo = new UndoManager();
Document doc = getDocument();
public ClipTextArea() {
doc.addUndoableEditListener(new UndoableEditListener(){
public void undoableEditHappened(UndoableEditEvent e) {
doc_addedit(e);}
});
addActionMap();
}
public void addActionMap()
{
getActionMap().put("Undo", new AbstractAction("Undo") {
public void actionPerformed(ActionEvent evt) {
try {
if (undo.canUndo()) {
undo.undo();
}
} catch (CannotUndoException e) {
}
}
});
getInputMap().put(KeyStroke.getKeyStroke("control Z"), "Undo");
getActionMap().put("Redo",new AbstractAction("Redo") {
public void actionPerformed(ActionEvent evt) {
try
{
if (undo.canRedo()) {
undo.redo();
}
} catch (CannotRedoException e) {
}
}
});
getInputMap().put(KeyStroke.getKeyStroke("control R"), "Redo");
getActionMap().put("Copy",new AbstractAction("Copy") {
public void actionPerformed(ActionEvent evt) {
copy();
}
});
getInputMap().put(KeyStroke.getKeyStroke("control C"), "Copy");
getActionMap().put("Cut",new AbstractAction("Cut") {
public void actionPerformed(ActionEvent evt) {
cut();
}
});
getInputMap().put(KeyStroke.getKeyStroke("control X"), "Cut");
getActionMap().put("Paste",new AbstractAction("Paste") {
public void actionPerformed(ActionEvent evt) {
paste();
}
});
getInputMap().put(KeyStroke.getKeyStroke("control V"), "Paste");
}
public void copy()
{
String py = getedText();
if (sCopy != null)
{
StringSelection sSelection = new StringSelection(sCopy);
strBoard.setContents(sSelection,this);
}
}
public void cut()
{
String sCopy = getSelectedText();
if (sCopy != null)
{
StringSelection sSelection = new StringSelection(sCopy);
strBoard.setContents(sSelection,this);
this.replaceSelection("");
}
}
public void paste()
{
Transferable sTransf = strBoard.getContents(this);
if (sTransf!=null)
{
try
{
String sPaste = (String)sTransf.getTransferData(DataFlavor.stringFlavor);
this.replaceRange(sPaste,getSelectionStart(),getSelectionEnd());
}
catch(UnsupportedFlavorException e)
{
}
catch(IOException ioe)
{
}
}
}
public void lostOwnership(Clipboard clipboard,Transferable contents)
{
}
void doc_addedit(UndoableEditEvent e)
{
undo.addEdit(e.getEdit());
}
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-998627/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- GUI quick cut and paste failedGUIASTAI
- Oracle Redo and UndoOracle Redo
- 【REDO】Oracle redo undo 學習Oracle Redo
- UNDO REDO 區別
- Java----【實現copy的功能】Java
- Redo 和 Undo 概念解析
- MySQL 日誌 undo | redoMySql
- undo log和redo log
- oracle的redo和undoOracle
- Oracle中的redo copy latchOracle
- 深入理解MYSQL undo redoMySql
- redo和undo的區別
- oracle redo和undo系列一Oracle Redo
- MySQL redo與undo日誌解析MySql
- Oracle redo undo commit rollback剖析Oracle RedoMIT
- Oracle中undo 如何產生RedoOracle
- 用 Java 實現回撥例程 (轉)Java
- SQLServer的檢查點、redo和undoSQLServer
- MySQL中的redo log和undo logMySql
- MySQL Undo Log和Redo Log介紹MySql
- Undo表空間與redo日誌
- redo與undo的一點點思考
- Oracle的redo 和undo的區別Oracle
- Vue手機端實現touch,paste指令VueAST
- 關於Oracle的redo和undo的理解Oracle
- 深入淺出-redo和undo記載01
- 深入淺出-redo和undo記載02
- 深入淺出redo和undo記載03
- 關於Undo地實現
- Java Tip: 用Reflection實現Visitor模式 (轉)Java模式
- 轉roger大師_計算sql語句產生的redo和undo大小SQL
- 淺析MySQL事務中的redo與undoMySql
- 用udp方式進行聊天的java實現. (轉)UDPJava
- 【expect】用expect實現scp/ssh-copy-id的非互動
- 【Mysql】三大日誌 redo log、bin log、undo logMySql
- oracle體系結構梳理---redo和undo解析1Oracle
- 【開發篇sql】 基礎概述(二) undo和redoSQL
- 用Java實現的設計模式系列(1)-Factory (轉)Java設計模式