【问题标题】:esacpe key exits full screen mode java退出键退出全屏模式java
【发布时间】:2023-03-19 06:25:01
【问题描述】:

我用 Java 构建了一个全屏模式的游戏,我使用这段代码来全屏:

this.setPreferredSize(new Dimension((int)screenSize.getWidth(), (int)screenSize.getHeight()));
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
        if(gd.isFullScreenSupported()) {
            this.setUndecorated(true);
            gd.setFullScreenWindow(this);
        }
        else {
            System.err.println("Full screen not supported!!");
            
        }

在某些 Windows 系统上,当我按下 ESCAPE 键时,程序将退出全屏模式并最小化。如何屏蔽这个“功能”?

【问题讨论】:

    标签: java fullscreen


    【解决方案1】:

    编写您自己的按键监听器,并以您想要的方式处理您的按键操作。你可以看看this

    编辑:this 似乎解释得更好。

    【讨论】:

      【解决方案2】:

      如果这是使用 Swing,那么您应该使用 Key Bindings。 Swing 旨在使用 KeyBindings,而不是 KeyEvents。

      【讨论】:

      • 我已经尝试过使用 KeyBindings AND keyListeners 来消费转义键事件,但是没有效果。
      【解决方案3】:

      我发现了问题,当我切换视图时,程序失去焦点,所以windows会处理ESCAPE键事件。我通过在每次视图更改时请求焦点来解决它。

      像这样:

      this.addComponentListener(new ComponentAdapter() {
                  @Override
                  public void componentShown(ComponentEvent e) {
                      View.this.requestFocusInWindow();
                  }            
              });
      

      【讨论】:

        猜你喜欢
        • 2012-03-29
        • 1970-01-01
        • 1970-01-01
        • 2014-12-15
        • 2017-11-16
        • 2023-04-03
        • 2014-10-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多