BUG:Java Swing键绑定在OSX中使用awt setFullScreenWindow在JDK 7中丢失函数

sag*_*e88 1 java macos awt key-bindings

编辑2013年1月16日:原始问题已被删除.这似乎是mac OSX上JDK 7的一个错误.我已经向Sun(Oracle)提交了一份错误报告.

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

有两个键绑定.按"ENTER"键应打印"按下Enter键".到stdout.按"ESCAPE"应该将"ESC Key终止的程序"打印到stdout并退出程序.

使用Windows 7 64和JDK Java SE 6和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);
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

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

this.setUndecorated(true);
GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(this);
Run Code Online (Sandbox Code Playgroud)

mic*_*den 5

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

有一个解决方法!

完全全屏后,请执行frame.setVisible(false);此操作frame.setVisible(true).它为什么有效?请参考以上链接.