如何使用JavaFX撤消/重做命令?

如何使用JavaFX撤消/重做命令?,java,javafx,command,Java,Javafx,Command,我已经实现了使用命令模式撤消/重做添加数字的功能,如下所示: public void start(Stage stage) { try { CommandManager command = new CommandManager(); command.addCommand(new CommandChanger("2")); command.undo(); command.redo(); } } 这是正确的撤销和重做

我已经实现了使用命令模式撤消/重做添加数字的功能,如下所示:

public void start(Stage stage) {
    try {
        CommandManager command = new CommandManager(); 
        command.addCommand(new CommandChanger("2"));
        command.undo();
        command.redo();
    }
}
这是正确的撤销和重做的2。但是,我如何通过单击JavaFX按钮来实现这一点呢

以下是我的CommandManager类,仅供参考:

public class CommandManager {
    private Node currentIndex = null;
    private Node parentNode = new Node();

    public CommandManager(){
        currentIndex = parentNode;
    }

    public CommandManager(CommandManager manager){
        this();
        currentIndex = manager.currentIndex;
    }

    public void clear(){
        currentIndex = parentNode;
    }
    public void addCommand(Command command){
        Node node = new Node(command);
        currentIndex.right = node;
        node.left = currentIndex;
        currentIndex = node;
    }

    public boolean canUndo(){
        return currentIndex != parentNode;
    }

    public boolean canRedo(){
        return currentIndex.right != null;
    }

    public void undo(){
        //validate
        if ( !canUndo() ){
            throw new IllegalStateException("Cannot undo. Index is out of range.");
        }
        //undo
        currentIndex.command.undo();
        //set index
        moveLeft();
    }

    private void moveLeft(){
        if ( currentIndex.left == null ){
            throw new IllegalStateException("Internal index set to null.");
        }
        currentIndex = currentIndex.left;
    }

    private void moveRight(){
        if ( currentIndex.right == null ){
            throw new IllegalStateException("Internal index set to null.");
        }
        currentIndex = currentIndex.right;
    }

    public void redo(){
        //validate
        if ( !canRedo() ){
            throw new IllegalStateException("Cannot redo. Index is out of range.");
        }
        //reset index
        moveRight();
        currentIndex.command.redo();
    }


    private class Node {
        private Node left = null;
        private Node right = null;
        private final Command command;
        public Node(Command c){
            command = c;
        }
        public Node(){
            command = null;
        }
    }
}

您是否可以从按钮的事件处理程序调用
command.undo()
?您是否可以从按钮的事件处理程序调用
command.undo()