欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java 输入框 undo 博客分类: Java javaswingundoUndoManager撤销 

程序员文章站 2024-02-24 19:12:58
...

类UndoTextArea:

package com.pass.component;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.text.Document;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.CannotUndoException;
import javax.swing.undo.UndoManager;

public class UndoTextArea extends JTextArea
{
    private static final long serialVersionUID = 2622113838910292609L;
    UndoManager undo = new UndoManager();
    Document    doc  = getDocument();

    public void stopUndo()
    {
//        undo.die();
        undo.discardAllEdits();
    }

    public UndoTextArea()
    {
        doc.addUndoableEditListener(new UndoableEditListener()
        {
            public void undoableEditHappened(UndoableEditEvent e)
            {
                undo.addEdit(e.getEdit());
            }
        });
        addActionMap();
    }

    public void addActionMap()
    {
        getActionMap().put("Undo", new AbstractAction("Undo11")
        {
            private static final long serialVersionUID = 2434402629308759912L;

            public void actionPerformed(ActionEvent evt)
            {
                try
                {
                    boolean b = undo.canUndo();
                    //                    System.out.println("whether undo : "+b);
                    if (b)
                    {
                        undo.undo();
                    }
                }
                catch (CannotUndoException e)
                {
                }
            }
        });
        getInputMap().put(KeyStroke.getKeyStroke("control Z"), "Undo");

        getActionMap().put("Redo", new AbstractAction("Redo1111")
        {
            private static final long serialVersionUID = 5348330289578410517L;

            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("Copy111")
        {
            private static final long serialVersionUID = -5151480809625853288L;

            public void actionPerformed(ActionEvent evt)
            {
                copy();
            }

        });
        getInputMap().put(KeyStroke.getKeyStroke("control C"), "Copy");

        getActionMap().put("Cut", new AbstractAction("Cut")
        {

            private static final long serialVersionUID = 7316612864835857713L;

            public void actionPerformed(ActionEvent evt)
            {
                cut();
            }

        });

        getInputMap().put(KeyStroke.getKeyStroke("control X"), "Cut");

        getActionMap().put("Paste", new AbstractAction("Paste111")
        {
            private static final long serialVersionUID = -3548620001691220571L;

            public void actionPerformed(ActionEvent evt)
            {
                paste();
            }
        });

        getInputMap().put(KeyStroke.getKeyStroke("control V"), "Paste");
    }

}
 

测试类:FrameMain

package com.pass.test;

import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;

import com.pass.component.UndoTextArea;

public class FrameMain extends JFrame
{
    private static final long serialVersionUID = 3526577815017461757L;
    public static void main(String[] args)
    {
        new FrameMain().launchFrame();
    }
    public void launchFrame(){
        JPanel pane=new JPanel();
        pane.setLayout(new GridLayout(2, 1));
        UndoTextArea txt1= new UndoTextArea();
        txt1.setBackground(new Color(255, 255, 100));
        txt1.setText("txt1");
        txt1.stopUndo();
        UndoTextArea txt2= new UndoTextArea();
        txt2.setBackground(new Color(255, 100, 100));
        txt2.setText("txt2");
        txt2.stopUndo();
        pane.add(txt1);
        pane.add(txt2);
        this.getContentPane().add(pane);
        this.setBounds(100, 100, 400, 400);
        this.setDefaultCloseOperation(3);
        this.setVisible(true);
    }

}
 

运行FrameMain,弹出swing窗口

随便输入什么内容,然后Ctrl+Z 就可以进行测试了。