我通过一个这样的接口,实现按ESC键时,窗口关闭,但是如果打开了多个窗口,会全部退出JAVA VM,而我需要的是只退出活动窗口,要怎么办啊? class LogonListener implements KeyEventPostProcessor {
public boolean postProcessKeyEvent(KeyEvent ke) {
if (ke.getKeyCode() == KeyEvent.VK_ENTER)
logon();
if (ke.getKeyCode() == KeyEvent.VK_ESCAPE)
dispose();
return true;
};
}

解决方案 »

  1.   

    首先你得窗口JFrame要设置
    setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    然后需要退出的时候dispose就可以了
      

  2.   

    DO_NOTHING_ON_CLOSE  DISPOSE_ON_CLOSE都可以
    dispose();
      

  3.   

    setDefaultCloseOperation
    public void setDefaultCloseOperation(int operation)Sets the operation that will happen by default when the user initiates a "close" on this frame. You must specify one of the following choices: DO_NOTHING_ON_CLOSE (defined in WindowConstants): Don't do anything; require the program to handle the operation in the windowClosing method of a registered WindowListener object. 
    HIDE_ON_CLOSE (defined in WindowConstants): Automatically hide the frame after invoking any registered WindowListener objects. 
    DISPOSE_ON_CLOSE (defined in WindowConstants): Automatically hide and dispose the frame after invoking any registered WindowListener objects. 
    EXIT_ON_CLOSE (defined in JFrame): Exit the application using the System exit method. Use this only in applications. 
    The value is set to HIDE_ON_CLOSE by default. Note: When the last displayable window within the Java virtual machine (VM) is disposed of, the VM may terminate. See AWT Threading Issues for more information. --------------------------
    JFrame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
    看看是不是这个啊
      

  4.   

    DISPOSE_ON_CLOSE的只是不用加窗体关闭(不是按cancel)的listener了 其他没什么
      

  5.   

    不是不是,我当然知道setDefaultCloseOperation(),但我要实现的功能是按某个键,比如ESC,就退出窗口,而不是通过button什么的
    我实现了按键退出,但只实现了一半,因为一按ESC就全部退出了,而不是退出当前的活动窗口