gpt4 book ai didi

java - (撤消/重做 || KeyStrokes)在 JEditorPane 中不起作用

转载 作者:行者123 更新时间:2023-12-01 09:28:55 25 4
gpt4 key购买 nike

我希望有人能提供帮助,因为我已经为此奋斗了太久,无法理解它。

我正在尝试在 JEditorPane 扩展类中实现撤消/重做(我在 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);
}

}

由于某种原因,我的击键没有被触发,或者撤消/重做不起作用。

我无法让它工作。有人可以向我指出一些事情吗?

最佳答案

当我发表评论时,你的代码似乎工作正常

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);
}
}

关于java - (撤消/重做 || KeyStrokes)在 JEditorPane 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39612658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com