如何在java中按Ctrl + z实现撤销

时间:2015-12-22 08:57:11

标签: java

我尝试编写TextEditor。现在我可以通过添加一个按钮来实现它。我想通过按Ctrl+Z来实现撤销。怎么处理呢? 请给我一些建议。

现在我可以通过

实现它
jta.addKeyListener(new KeyListener() {

    @Override
    public void keyTyped(KeyEvent e) {
        if((e.getKeyChar() == 0x1a)) {
            if(doManager.canUndo()) {
                doManager.undo();
            }
            System.out.println(1);
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {

    }
});

为什么这不能?

jta.addKeyListener(new KeyListener() {

    @Override
    public void keyTyped(KeyEvent e) {
        if((e.getKeyCode() == KeyEvent.VK_Z) && ((e.getModifiers() & KeyEvent.CTRL_MASK) != 0)) {
            if(doManager.canUndo()) {
                doManager.undo();
            }
            System.out.println(1);
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {

    }
});

为什么会这样?

jta.addKeyListener(new KeyListener() {

    @Override
    public void keyTyped(KeyEvent e) {
        if((e.getKeyCode() == KeyEvent.VK_Z) && (e.isControlDown() == true)) {
            if(doManager.canUndo()) {
                doManager.undo();
            }
            System.out.println(1);
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {

    }
});

@Felix Gerber

2 个答案:

答案 0 :(得分:2)

可行的方法是在用户界面中添加KeyListener

Oracle有一个关于编写自己的here的小教程。

以下是一个小例子,检查CRTL+Z是否被JFrame压缩:

    JFrame frame = new JFrame();
    frame.addKeyListener(new KeyListener() {

        @Override
        public void keyTyped(KeyEvent e) {


        }

        @Override
        public void keyReleased(KeyEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void keyPressed(KeyEvent e) {
            if ((e.getKeyCode() == KeyEvent.VK_Z) && ((e.getModifiers() & KeyEvent.CTRL_MASK) != 0)) 
            {
                System.out.println("Your code here!");
            }
        }
    });
    frame.setLocationRelativeTo(null);
    frame.setSize(200, 200);
    frame.setVisible(true);

答案 1 :(得分:1)

谢谢大家!

现在我可以通过

实现它
jta.addKeyListener(new KeyListener() {

    @Override
    public void keyTyped(KeyEvent e) {
        if((e.getKeyChar() == 0x1a)) {
            if(doManager.canUndo()) {
                doManager.undo();
            }
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {

    }
});

jta.addKeyListener(new KeyListener() {

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        if((e.getKeyCode() == KeyEvent.VK_Z) && ((e.getModifiers() & KeyEvent.CTRL_MASK) != 0)) {
            if(doManager.canUndo()) {
                doManager.undo();
            }
        }
    }
});

jta.addKeyListener(new KeyListener() {

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        if((e.getKeyCode() == KeyEvent.VK_Z) && (e.isControlDown() == true)) {
            if(doManager.canUndo()) {
                doManager.undo();
            }
        }
    }
});