在Java中处理键盘键ALT + F4组合 [英] Handle Keyboard Keys ALT+F4 combination in Java
本文介绍了在Java中处理键盘键ALT + F4组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是一名Windows用户,当我按
ALT + F4 时,我不想关闭我的jframe,
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){
}
}
获取击键并处理/停止关闭或切换jframe
get the keystrokes and handle/stop closing or switching of the jframe
所以如何在java中处理这个keypressed组合,
请帮助....
so how can i handle this keypressed combination in java, please help....
推荐答案
<如果确实你按下了组合键意味着即使关闭X也不起作用,前一个将锁定关闭。因此以下
审查:基于快速蜗牛回答更好:
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();
}
}
这篇关于在Java中处理键盘键ALT + F4组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文