java redo_java undo redo

package org.alvin.chapter2;

import java.awt.EventQueue;

public class UndoFrame extends JFrame {

private JPanel contentPane;

private JTree tree;

private KeyStroke restroe_key = KeyStroke.getKeyStroke("control Y");

private KeyStroke back_key = KeyStroke.getKeyStroke("control Z");

private UndoManager undo = new UndoManager();

private RedoEditAction redoAction = new RedoEditAction("恢复");

private UndoAction undoAction = new UndoAction("撤销");

/**

* Launch the application.

*/

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {

try {

UndoFrame frame = new UndoFrame();

frame.setVisible(true);

} catch (Exception e) {

e.printStackTrace();

}

}

});

}

/**

* Create the frame.

*/

public UndoFrame() {

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setBounds(100, 100, 632, 456);

contentPane = new JPanel();

contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

setContentPane(contentPane);

GridBagLayout gbl_contentPane = new GridBagLayout();

gbl_contentPane.columnWidths = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

gbl_contentPane.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 39,

0, 0 };

gbl_contentPane.columnWeights = new double[] { 1.0, 0.0, 0.0, 0.0, 0.0,

0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };

gbl_contentPane.rowWeights = new double[] { 1.0, 0.0, 0.0, 0.0, 0.0,

0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };

contentPane.setLayout(gbl_contentPane);

JScrollPane scrollPane = new JScrollPane();

GridBagConstraints gbc_scrollPane = new GridBagConstraints();

gbc_scrollPane.gridheight = 10;

gbc_scrollPane.gridwidth = 9;

gbc_scrollPane.insets = new Insets(0, 0, 5, 0);

gbc_scrollPane.fill = GridBagConstraints.BOTH;

gbc_scrollPane.gridx = 0;

gbc_scrollPane.gridy = 0;

contentPane.add(scrollPane, gbc_scrollPane);

MyTextArea textArea = new MyTextArea();

scrollPane.setViewportView(textArea);

JButton button_1 = new JButton("撤销");

GridBagConstraints gbc_button_1 = new GridBagConstraints();

gbc_button_1.anchor = GridBagConstraints.NORTH;

gbc_button_1.insets = new Insets(0, 0, 0, 5);

gbc_button_1.gridx = 5;

gbc_button_1.gridy = 10;

contentPane.add(button_1, gbc_button_1);

JButton button = new JButton("恢复");

GridBagConstraints gbc_button = new GridBagConstraints();

gbc_button.anchor = GridBagConstraints.NORTH;

gbc_button.insets = new Insets(0, 0, 0, 5);

gbc_button.gridx = 7;

gbc_button.gridy = 10;

contentPane.add(button, gbc_button);

injectAction(textArea);

textArea.getDocument().addUndoableEditListener(undo);

button.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

if (undo.canRedo()) {

undo.redo();

} else {

System.out.println("没有恢复的了");

}

}

});

button_1.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

if (undo.canUndo()) {

undo.undo();

} else {

System.out.println("没有撤销的了");

}

}

});

}

abstract class KeyAction extends AbstractAction {

@Override

public abstract boolean isEnabled();

@Override

protected void firePropertyChange(String propertyName, Object oldValue,

Object newValue) {

boolean value = isEnabled();

super.firePropertyChange("enabled", Boolean.valueOf(!value),

Boolean.valueOf(value));

}

}

public void injectAction(JComponent component) {

InputMap input = component.getInputMap();

ActionMap action = component.getActionMap();

action.put("redo", redoAction);

input.put(restroe_key, "redo");

action.put("undo", undoAction);

input.put(back_key, "undo");

}

class RedoEditAction extends AbstractAction {

private static final long serialVersionUID = 1L;

RedoEditAction(String text) {

super(text);

}

RedoEditAction(String text, Icon icon) {

super(text, icon);

}

@Override

public boolean isEnabled() {

return undo.canRedo();

}

@Override

public void actionPerformed(ActionEvent e) {

if (undo.canRedo()) {

undo.redo();

}

}

}

class UndoAction extends AbstractAction {

public UndoAction(String text) {

super(text);

}

public UndoAction(String text, Icon icon) {

super(text, icon);

}

@Override

public void actionPerformed(ActionEvent e) {

if (undo.canUndo()) {

undo.undo();

}

}

}

class MyTextArea extends JTextArea implements DropTargetListener {

public MyTextArea() {

new DropTarget(this, DnDConstants.ACTION_COPY_OR_MOVE, this);

}

public void dragEnter(DropTargetDragEvent dtde) {

}

public void dragOver(DropTargetDragEvent dtde) {

}

public void dropActionChanged(DropTargetDragEvent dtde) {

}

public void dragExit(DropTargetEvent dte) {

}

public void drop(DropTargetDropEvent dtde) {

try {

Transferable tr = dtde.getTransferable();

if (dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {

dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);

System.out.println("file cp");

List list = (List) (dtde.getTransferable()

.getTransferData(DataFlavor.javaFileListFlavor));

Iterator iterator = list.iterator();

while (iterator.hasNext()) {

File f = (File) iterator.next();

this.setText(f.getAbsolutePath());

}

dtde.dropComplete(true);

this.updateUI();

} else {

dtde.rejectDrop();

}

} catch (IOException ioe) {

ioe.printStackTrace();

} catch (UnsupportedFlavorException ufe) {

ufe.printStackTrace();

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值