我在我的程序中调用了一个新阶段,我希望在按下escape时关闭.我这样做了,它给了我一个NullPointerException:
scene.setOnKeyPressed(new EventHandler<KeyEvent>() { @Override public void handle(KeyEvent t) { KeyCode key = t.getCode(); if (key == KeyCode.ESCAPE){ stage.close(); } } });
解决方法
试试这个..
scene.addEventHandler(KeyEvent.KEY_PRESSED,new EventHandler<KeyEvent> () { @Override public void handle(KeyEvent t) { if(t.getCode()==KeyCode.ESCAPE) { System.out.println("click on escape"); Stage sb = (Stage)label.getScene().getWindow();//use any one object sb.close(); } } });