I am a windows user and i don't want to close my jframe when I press
ALT+F4,
public void keyPressed(KeyEvent e) {
if (KeyEvent.VK_SPACE, java.awt.event.InputEvent.CTRL_DOWN_MASK){
}
}
get the keystrokes and handle/stop closing or switching of the jframe
so how can i handle this keypressed combination in java,
please help....
解决方案
the previous will lock closing if indeed you press the key combination meaning there on even close X will not work. hence the following
REVIEW: much better based on fast snail answer:
public class OnKeyAltF4DontClose2 extends JFrame {//implements WindowListener {
public OnKeyAltF4DontClose2() {
setVisible(true);
setDefaultCloseOperation(3);
setBounds(400,400,400,400);
addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e) {
if(((KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, java.awt.event.InputEvent.ALT_DOWN_MASK)) != null) && e.getKeyCode() == KeyEvent.VK_F4){
e.consume();
}
}
});
}
public static void main(String[] args) {
new OnKeyAltF4DontClose2();
}
}