BUG:在带有awt setFullScreenWindow的OSX中使用JDK 7的Java Swing键绑定失去功能

编辑1/16/2013:原始问题已删除.这似乎是Mac OSX上JDK 7的错误.我已经向Sun(Oracle)提交了错误报告.

下面的文件使用awt类GraphicsEnvironment和setFullScreenWindow方法将图像显示为全屏.没有图像,因此运行代码时屏幕将为灰色.但是,键绑定仍应起作用.

有两个键绑定.按下“ ENTER”(回车)应打印“按下了Enter”.到标准输出.按“ ESCAPE”将打印“由ESC键终止的程序”以标准输出并退出该程序.

使用Windows 7 64和JDK Java SE 6 AND 7,这些键绑定可以按预期工作.

使用Mac OSX 10.7 Lion和JDK Java SE 6,这些键绑定可以按预期工作.

使用Mac OSX 10.7 Lion和JDK Java SE 7,这些键绑定将停止工作.

回滚到JDK Java SE 6会使它们重新开始工作.

我不知道它是否会影响其他操作系统.

我已经尝试了所有版本的JComponent.WHEN_IN_FOCUS等…并且这些选项都不能解决问题.

下面是一个SSCCE,仅当您使用Mac OSX 10.7和JDK Java SE 7时,该错误才会重现该错误.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class FullScreen extends JFrame
{
    /*
     * screenImage is never set in this code. It can be set to any image
     * the error will still be present. Images have been omitted to simplify
     * the example case.
     */
    private Image screenImage;
    private int width;
    private int height;

    //Create panel for displaying images using paintComponent()
    private PaintPanel mainImagePanel;

    //Used for keybinding
    private Action enterAction;
    private Action escapeAction;
    private static final String enter = "ENTER";
    private static final String escape = "ESCAPE";

    public FullScreen()
    {
 /**********************************************
  ******THE BELOW LINES CAUSE THE ERROR*********
  **********************************************/

        /****************************************** 
         * Removes window framing and sets fullscreen mode.
         ******************************************/

        this.setUndecorated(true);
        GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(this);

 /**********************************************
  ******THE ABOVE LINES CAUSE THE ERROR*********
  **********************************************/

        width = this.getWidth();
        height = this.getHeight();

        //Create panel so that I can use key binding which requires JComponent
        mainImagePanel = new PaintPanel();      
        add(mainImagePanel);

        /****************************************** 
         * Key Binding
         ******************************************/

        // Key bound AbstractAction items 
        enterAction = new EnterAction();
        escapeAction = new EscapeAction();

        // Gets the mainImagePanel InputMap and pairs the key to the action
        mainImagePanel.getInputMap().put(KeyStroke.getKeyStroke(enter), "doEnterAction");
        mainImagePanel.getInputMap().put(KeyStroke.getKeyStroke(escape), "doEscapeAction");

        // This line pairs the AbstractAction enterAction to the action "doEnterAction"
        mainImagePanel.getActionMap().put("doEnterAction", enterAction);
        mainImagePanel.getActionMap().put("doEscapeAction", escapeAction);

        /******************************************
         * End Key Binding
         ******************************************/
    }

    //Stretches and displays images in fullscreen window
    private class PaintPanel extends JPanel
    {
        @Override
        public void paintComponent(Graphics g) 
        { 
            if(screenImage != null)
            {
                super.paintComponent(g);
                g.drawImage(screenImage, 0, 0, width, height, this);
            }  
        }
    }

    /******************************************
     * User Input
     ******************************************/

    private class EnterAction extends AbstractAction
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            System.out.println("Enter was pressed.");
        }
    }

    private class EscapeAction extends AbstractAction
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            System.out.println("Program Terminated by ESC Key");
            System.exit(0);
        }
    }

    public static void main(String[] args) 
    {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() 
            {
                FullScreen show = new FullScreen();
                show.setVisible(true);
            }
        });
    }
}

所以下面两行引起了问题.

this.setUndecorated(true);
GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(this);

解决方法:

http://mail.openjdk.java.net/pipermail/macosx-port-dev/2012-November/005109.html

有一个解决方法!

全屏显示后,执行frame.setVisible(false);.然后是frame.setVisible(true).为什么行得通?请查阅上面的链接.

上一篇:Java最终类中受保护方法的用例是什么?


下一篇:java-将BufferedImage像素数据转换为可读输出?