posts - 38,  comments - 22,  trackbacks - 0

package urdo;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.CannotUndoException;
import javax.swing.undo.UndoManager;

public class NewJFrame extends javax.swing.JFrame {
 private JPanel jp1;

 private JButton jb1;

 private JButton jb2;

 private JMenuItem jm12;

 private JMenuItem jm11;

 private JMenu jm;

 private JMenuBar jMenuBar1;

 private JTextArea jta;

 private JScrollPane jsp;

 /**
  * Auto-generated main method to display this JFrame
  */
 public static void main(String[] args) {
  NewJFrame inst = new NewJFrame();
  inst.setVisible(true);
 }

 public NewJFrame() {
  super();
  initGUI();
 }

 private void initGUI() {
  try {
   setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
   {
    jMenuBar1 = new JMenuBar();
    setJMenuBar(jMenuBar1);
    {
     jm = new JMenu();
     jMenuBar1.add(jm);
     jm.setText("jm");
     {
      jm11 = new JMenuItem(redoAction);
      jm.add(jm11);
      jm11.setText("re");
     }
     {
      jm12 = new JMenuItem(undoAction);
      jm.add(jm12);
      jm12.setText("undo");
     }
     jm12.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,
       InputEvent.CTRL_MASK));
    }
   }
   {
    jp1 = new JPanel();
    getContentPane().add(jp1, BorderLayout.NORTH);
    jp1.setPreferredSize(new java.awt.Dimension(392, 35));
    {
     jb1 = new JButton(redoAction);
     jp1.add(jb1);
     jb1.setText("redo");
    }
    //    jb1.registerKeyboardAction(redoAction, KeyStroke.getKeyStroke(
    //      KeyEvent.VK_Y, KeyEvent.CTRL_DOWN_MASK, true),
    //      JComponent.WHEN_IN_FOCUSED_WINDOW);
    {
     jb2 = new JButton(undoAction);
     jp1.add(jb2);
     jb2.setText("undo");
    }
    //    jb2.registerKeyboardAction(undoAction, KeyStroke.getKeyStroke(
    //      KeyEvent.VK_Z, KeyEvent.CTRL_DOWN_MASK, true),
    //      JComponent.WHEN_IN_FOCUSED_WINDOW);

   }
   {
    jsp = new JScrollPane();
    getContentPane().add(jsp, BorderLayout.CENTER);
    {
     jta = new JTextArea();
     jsp.setViewportView(jta);
     jta.getDocument().addUndoableEditListener(undoHandler);
    }
   }
   pack();
   setSize(400, 300);
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 protected UndoableEditListener undoHandler = new UndoHandler();

 protected UndoManager undo = new UndoManager();

 private UndoAction undoAction = new UndoAction();

 private RedoAction redoAction = new RedoAction();

 class UndoHandler implements UndoableEditListener {

  /**
   * Messaged when the Document has created an edit, the edit is
   * added to <code>undo</code>, an instance of UndoManager.
   */
  public void undoableEditHappened(UndoableEditEvent e) {
   undo.addEdit(e.getEdit());
   undoAction.update();
   redoAction.update();
  }
 }

 class UndoAction extends AbstractAction {
  public UndoAction() {
   super();
   setEnabled(false);
  }

  public void actionPerformed(ActionEvent e) {
   try {
    undo.undo();
   } catch (CannotUndoException ex) {
    ex.printStackTrace();
   }
   update();
   redoAction.update();
  }

  protected void update() {
   if (undo.canUndo()) {
    setEnabled(true);
    //    System.out.println(undo.getUndoPresentationName());
    putValue(Action.NAME, undo.getUndoPresentationName());
   } else {
    setEnabled(false);
    putValue(Action.NAME, "撤消");
   }
  }
 }

 class RedoAction extends AbstractAction {
  public RedoAction() {
   super();
   setEnabled(false);
  }

  public void actionPerformed(ActionEvent e) {
   try {
    undo.redo();
   } catch (CannotRedoException ex) {
    ex.printStackTrace();
   }
   update();
   undoAction.update();
  }

  protected void update() {
   if (undo.canRedo()) {
    setEnabled(true);
    putValue(Action.NAME, undo.getRedoPresentationName());
   } else {
    setEnabled(false);
    putValue(Action.NAME, "重做");
   }
  }
 }
}

posted on 2007-01-25 09:46 aaabbb 阅读(689) 评论(1)  编辑  收藏 所属分类: Swing

只有注册用户登录后才能发表评论。


网站导航: