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

2023-12-08

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

下面的文件使用 awt 类 GraphicsEnvironment 和方法 setFullScreenWindow 将图像显示为全屏。不包含图像,因此运行代码时屏幕将呈灰色。然而,按键绑定应该仍然有效。

有两个键绑定。按“ENTER”应该打印“Enter was Pressed”。到标准输出。按“ESCAPE”应将“Program Termminated by ESC Key”打印到标准输出并退出程序。

使用 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 这些键绑定stop在职的。

回滚到 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); then frame.setVisible(true)。为什么它有效?请参阅上面的链接。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

BUG:Java Swing 键绑定在 OSX 中使用 awt setFullScreenWindow 时 JDK 7 失去功能 的相关文章

随机推荐