how to escape from full screen mode in java?

…衆ロ難τιáo~ 提交于 2019-12-12 02:09:58

问题


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);
}

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?


回答1:


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.




回答2:


Try this

 KeyStroke k = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
 int w = full.WHEN_IN_FOCUSED_WINDOW;
 dialog.getRootPane().registerKeyboardAction(e -> window.dispose(), k, w);


来源:https://stackoverflow.com/questions/38802417/how-to-escape-from-full-screen-mode-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!