如何在Java中从全屏模式转义? [英] how to escape from full screen mode in java?
问题描述
我已使用以下代码进入全屏显示:
I have used this code to go full screen:
private void fullScreenPerformed(java.awt.event.ActionEvent evt) {
full = new JFrame();
full.setSize(Toolkit.getDefaultToolkit().getScreenSize());
full.setUndecorated(true);
full.setVisible(true);
}
运行程序时,JFrame卡在全屏模式下,当按Escape键时无法将其关闭.因此,我必须重新启动或注销计算机才能再次回到正常屏幕.我希望用户能够通过按退出按钮"或使用其他组合来关闭它.我该怎么办?
When I run the program, the JFrame is stuck in the full-screen mode, and I can't close it when I press escape. Thus, I had to restart or log out of my computer to get back to normal screen again. I want the user to be able to close it by pressing the "escape button" or using other combinations. How can I do so?
推荐答案
您需要为此添加一个keyEventListener
,如果您按了转义键,它将放置框架.之后,它将不再可用.
You need to add an keyEventListener
for that, which disposes the frame if you have pressed escape. After that, it will not be usable again.
full.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent evt) {
if(evt.getKeyCode() == KeyEvent.VK_ESCAPE)
full.dispose();
}
});
请记住,框架需要聚焦,否则事件将不会触发.
Keep in mind that the frame needs to be focused, or else the event is not fired.
这篇关于如何在Java中从全屏模式转义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!