我正在尝试向我的 JPanel 添加一个 keylistener,但即使在此之后它也无法工作:
put.setFocusable(true);
put.requestFocusInWindow();
KeyboardHandler keyhandler=new KeyboardHandler();
put.addKeyListener(keyhandler);
}
private class KeyboardHandler implements KeyListener{
public void keyPressed(KeyEvent e) {
System.out.println("OVDE ZZZ");
if(e.getKeyCode()==17) ctrl=true;
if(e.getKeyCode()==90) z=true;
if(ctrl && z){
if (UndoBrojac==0) JOptionPane.showMessageDialog(null, "You can't undo that");
else{
UndoBrojac--;
put.setUndo(UndoBrojac);
}
}
}
public void keyReleased(KeyEvent e){
if (e.getKeyCode()==17) ctrl = false;
if (e.getKeyCode()==90) z = false;
}
public void keyTyped(KeyEvent e) {}
}
我正在尝试添加 ctrl+z 但它不起作用。