JTextArea的undo与redo

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.undo.UndoManager;


public class Test extends JFrame{
 private JTextArea text;
 private JButton un,re;
 private UndoManager und;
 private Container container;
 
 public Test(){
  super("Undo&Redo");
  und = new UndoManager();
  container = getContentPane();
  text =new JTextArea(10,15);
  text.setWrapStyleWord(true);
  text.getDocument().addUndoableEditListener(und);
  un = new JButton("撤销");
  un.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent arg0){
    if (und.canUndo())
     und.undo();

   }
   }
  );
  re = new JButton("重做");

  re.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent arg0){
    if (und.canRedo())
     und.redo();

   }
   }
  );

JPanel panel = new JPanel();

panel.setLayout(new FlowLayout());

panel.add(un);

panel.add(re);
  container.add(text);
  container.add(panel,BorderLayout.SOUTH);
  
  setVisible(true);
  pack();
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
  
 }
 public static void main(String args[]){
  Test test = new Test();
 }
}  

你可能感兴趣的:(JTextArea的undo与redo)