我希望有人能帮助我,因为我已经为这个问题斗争了太久,我的大脑无法理解它。
我正在尝试在JEditorPane扩展类中实现Undo/Redo (我在http://alvinalexander.com/java/java-undo-redo找到的):
public class TextEditor extends JEditorPane {
class UndoHandler implements UndoableEditListener {
@Override
public void undoableEditHappened(UndoableEditEvent e) {
undoManager.addEdit(e.getEdit());
undoAction.update();
redoAction.update();
}
}
class UndoAction extends AbstractAction {
public UndoAction() {
super("Undo");
setEnabled(false);
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("UNDO!");
try {
undoManager.undo();
} catch (CannotUndoException ex) {
// TODO deal with this
ex.printStackTrace();
}
update();
redoAction.update();
}
protected void update() {
if (undoManager.canUndo()) {
setEnabled(true);
putValue(Action.NAME, undoManager.getUndoPresentationName());
} else {
setEnabled(false);
putValue(Action.NAME, "Undo");
}
}
}
class RedoAction extends AbstractAction {
public RedoAction() {
super("Redo");
setEnabled(false);
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("REDO!");
try {
undoManager.redo();
} catch (CannotRedoException ex) {
ex.printStackTrace();
}
update();
undoAction.update();
}
protected void update() {
if (undoManager.canRedo()) {
setEnabled(true);
putValue(Action.NAME, undoManager.getRedoPresentationName());
} else {
setEnabled(false);
putValue(Action.NAME, "Redo");
}
}
}
private UndoHandler undoHandler = new UndoHandler();
private UndoManager undoManager = new UndoManager();
private UndoAction undoAction = new UndoAction();
private RedoAction redoAction = new RedoAction();
public TextEditor() {
super();
this.setEditorKit(new ShowSpecCharsEditorKit());
this.getDocument().addUndoableEditListener(undoHandler);
KeyStroke undoKeystroke = KeyStroke.getKeyStroke(KeyEvent.VK_Z, Event.CTRL_MASK);
KeyStroke redoKeystroke = KeyStroke.getKeyStroke(KeyEvent.VK_Y, Event.CTRL_MASK);
this.getInputMap().put(undoKeystroke, "undoKeystroke");
this.getActionMap().put("undoKeystroke", undoAction);
this.getInputMap().put(redoKeystroke, "redoKeystroke");
this.getActionMap().put("redoKeystroke", redoAction);
this.addCaretListener(new CaretListener() {
@Override
public void caretUpdate(CaretEvent e) {
((EditorTab)getParent().getParent()).updateTabTitle(true);
}
});
}
@Override
public void read(Reader r, Object desc) throws IOException{
super.read(r, desc);
}
}由于某些原因,我的按键没有被触发,或者撤销/重做就是不起作用。
我不能让它工作。有人能给我指出点什么吗?
发布于 2016-09-21 17:54:54
当我评论的时候,你的代码看起来运行得很好
来自您的代码的this.setEditorKit(new ShowSpecCharsEditorKit());
这可能是编辑器工具包的问题,请检查keyStrokes和Actions上的自定义EditorKit (ShowSpecCharsEditorKit)实现的代码。
public class TextEditor extends JEditorPane {
class UndoHandler implements UndoableEditListener {
@Override
public void undoableEditHappened(UndoableEditEvent e) {
undoManager.addEdit(e.getEdit());
undoAction.update();
redoAction.update();
}
}
class UndoAction extends AbstractAction {
public UndoAction() {
super("Undo");
setEnabled(false);
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("UNDO!");
try {
undoManager.undo();
} catch (CannotUndoException ex) {
// TODO deal with this
ex.printStackTrace();
}
update();
redoAction.update();
}
protected void update() {
if (undoManager.canUndo()) {
setEnabled(true);
putValue(Action.NAME, undoManager.getUndoPresentationName());
} else {
setEnabled(false);
putValue(Action.NAME, "Undo");
}
}
}
class RedoAction extends AbstractAction {
public RedoAction() {
super("Redo");
setEnabled(false);
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("REDO!");
try {
undoManager.redo();
} catch (CannotRedoException ex) {
ex.printStackTrace();
}
update();
undoAction.update();
}
protected void update() {
if (undoManager.canRedo()) {
setEnabled(true);
putValue(Action.NAME, undoManager.getRedoPresentationName());
} else {
setEnabled(false);
putValue(Action.NAME, "Redo");
}
}
}
private UndoHandler undoHandler = new UndoHandler();
private UndoManager undoManager = new UndoManager();
private UndoAction undoAction = new UndoAction();
private RedoAction redoAction = new RedoAction();
public TextEditor() {
super();
// this.setEditorKit(new ShowSpecCharsEditorKit());
this.getDocument().addUndoableEditListener(undoHandler);
KeyStroke undoKeystroke = KeyStroke.getKeyStroke(KeyEvent.VK_Z, Event.CTRL_MASK);
KeyStroke redoKeystroke = KeyStroke.getKeyStroke(KeyEvent.VK_Y, Event.CTRL_MASK);
this.getInputMap().put(undoKeystroke, "undoKeystroke");
this.getActionMap().put("undoKeystroke", undoAction);
this.getInputMap().put(redoKeystroke, "redoKeystroke");
this.getActionMap().put("redoKeystroke", redoAction);
this.addCaretListener(new CaretListener() {
@Override
public void caretUpdate(CaretEvent e) {
// ((EditorTab)getParent().getParent()).updateTabTitle(true);
}
});
}
@Override
public void read(Reader r, Object desc) throws IOException {
super.read(r, desc);
}
public static void main(String[] args) {
JFrame jframe = new JFrame();
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setSize(500, 500);
jframe.add(new TextEditor());
jframe.setVisible(true);
}
}https://stackoverflow.com/questions/39612658
复制相似问题