我通过一个这样的接口,实现按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;
};
}
public boolean postProcessKeyEvent(KeyEvent ke) {
if (ke.getKeyCode() == KeyEvent.VK_ENTER)
logon();
if (ke.getKeyCode() == KeyEvent.VK_ESCAPE)
dispose();
return true;
};
}
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
然后需要退出的时候dispose就可以了
dispose();
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);
看看是不是这个啊
我实现了按键退出,但只实现了一半,因为一按ESC就全部退出了,而不是退出当前的活动窗口