Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Undo, Redo,Copy, Paste and Cut with JAVA

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to use JAVA to achieve Undo, Redo,Copy, Paste, Cut, I hope you will learn something after reading this article, let's discuss it together!

Package clipborad

Import Javax.swing.JTextArea

Import java.awt.datatransfer.*

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: implement Undo, Redo,Copy, Paste and Cut with JAVA

* Description: implement Undo, Redo,Copy, Paste and Cut with JAVA

* Copyright: Copyright (c) 2001

* Company:

* @ author: Peng Jianxiong

* @ version 1.0

, /

Public class ClipTextArea extends JTextArea implements ClipboardOwner {

Clipboard strBoard = this.getToolkit () .getSystemClipboard ()

Undomanager 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 SCOpy = getselectedText ()

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 (sTransfenced 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 ())

}

}

After reading this article, I believe you have a certain understanding of "how to use JAVA to achieve Undo, Redo,Copy, Paste, Cut". If you want to know more related knowledge, welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report