在Mac OS X上使用键盘输入进行Java Swing全屏显示 [英] Java Swing fullscreen with keyboard input on Mac OS X

查看:162
本文介绍了在Mac OS X上使用键盘输入进行Java Swing全屏显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将Java JFrame在所有操作系统(Windows,Mac,Linux)上全屏显示时遇到了一些问题.看来,无论我发现哪种解决方案都可以在一个操作系统上运行,但不能在其他操作系统上运行,或者有其他严重的错误.我想使用setFullScreenWindow(window w)方法正确启动全屏,因为setExtendedState(...)在Mac/Linux上不起作用,因为菜单栏和任务栏仍然可见.

I'm having some issues getting my Java JFrame to be fullscreen on all OS (Windows, Mac, Linux). It seems whatever solution I find it does run on one OS but not on others or has some other serious bugs. I wanted to use the setFullScreenWindow(window w) method to properly initiate a fullscreen because setExtendedState(...) won't work on Mac/Linux as the menubar and taskbar are still visible.

在Java 7出现之前,setFullScreenWindow(...)方法在所有环境下都能正常工作,现在似乎存在一个问题,即一旦进入全屏模式,应用程序就不再响应Mac OS X上的按键事件.该应用程序在Windows上可以正常运行.

The setFullScreenWindow(...) method worked fine on all environments until Java 7 came along and now there seems to be an issue that as soon as you enter fullscreen mode the application no longer responds to key events on Mac OS X. The application works just fine on Windows.

有人知道我如何解决这个问题吗?

Does anyone have a clue how I could possibly work around this issue?

注意: 此处描述的解决方法(

Note: The workaround described here (FullScreen Swing Components Fail to Receive Keyboard Input on Java 7 on Mac OS X Mountain Lion) does not work for Windows because it will result in the JFrame flickering and not opening properly.

此处描述的全屏方式与下面使用的方式相同,由于按键输入存在问题,因此无法使用:( 示例代码:

import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.KeyStroke;

public class FullScreenKeyTest extends JFrame {

public void createFrame() {
    initKey();
    GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
    this.setUndecorated(true);
    this.setVisible(true);
    gd.setFullScreenWindow(this);
}

private void initKey() {
    Action keyAction = new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("Escape key pressed");
            setVisible(false);
            System.exit(0);
        }
    };
    this.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "keyPress");
    this.getRootPane().getActionMap().put("keyPress", keyAction);
}

public static void main(String[] args) {
    FullScreenKeyTest testFrame = new FullScreenKeyTest();
    testFrame.createFrame();
}
}

推荐答案

这有点不稳定,因为我可以同时使用它并破坏它.

This is a bit flaky as I can get it to work and break it at the same time.

我添加了您的示例代码

getContentPane().setFocusable(true);
getContentPane().requestFocus();

createFrame方法的末尾,我没有对根窗格注册操作,而是对内容窗格注册了

To the end of the createFrame method, and instead of registering the action against the root pane, I registered against the content pane

这篇关于在Mac OS X上使用键盘输入进行Java Swing全屏显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆