BUG:Java Swing的按键绑定在OSX失去功能的JDK 7 AWT setFullScreenWindow [英] BUG: Java Swing Key Bindings Lose Function with JDK 7 in OSX with awt setFullScreenWindow

查看:156
本文介绍了BUG:Java Swing的按键绑定在OSX失去功能的JDK 7 AWT setFullScreenWindow的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑2013年1月16日:原来的问题已被删除。这似乎是在Mac OSX上的JDK 7的错误。我已经申请一个bug报告与Sun(甲骨文)。

的文件下使用AWT类GraphicsEnvironment中,并且该方法setFullScreenWindow以显示图像到全屏模式。无图像都包括在内,因此运行code时,屏幕上会显示为灰色。关键绑定应该,但是,仍然工作。

有两个关键的绑定。 pressingENTER应打印进入了pressed。到标准输出。 pressing逃离应打印程序通过ESC键终止到标准输出和退出程序。

使用Windows 7 64和JDK的Java SE 6和7,这些键绑定正常工作。

使用Mac OSX 10.7狮子和JDK的Java SE

6,这些键绑定正常工作。

使用Mac OSX 10.7狮子和JDK的Java SE 7这些键绑定的停止工作

回滚到JDK的Java SE 6使他们重新开始工作。

我不知道这是否会影响其它操作系统。

我已经试过JComponent.WHEN_IN_FOCUS等的所有版本......和这些选择都不解决问题。

下面是一个 SSCCE 将重现只有当你使用的是Mac OSX 10.7和JDK的Java SE 7 <错误/ p>

 进口的javax.swing *。
进口java.awt中的*。
java.awt.event中导入*。公共类全屏扩展的JFrame
{
    / *
     * screenImage永远不会在这个code设置。它可以被设置为任何图像
     *错误仍然将是present。简化图像已被忽略
     *的示例情况。
     * /
    私人形象screenImage;
    私人诠释宽度;
    私人诠释高度;    //创建用于显示使用的paintComponent图像面板()
    私人PaintPanel mainImagePanel;    //用于键绑定
    私人诉讼enterAction;
    私人诉讼escapeAction;
    私有静态最终字符串输入=ENTER;
    私有静态最后弦乐逃逸=ESCAPE;    公共全屏()
    {
 / **********************************************
  ******下面的线路导致错误*********
  ********************************************** /        / ******************************************
         *删除窗口框架,并设置全屏模式。
         ****************************************** /        this.setUndecorated(真);
        GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(this); / **********************************************
  ******上面的行会导致错误*********
  ********************************************** /        宽度= this.getWidth();
        高度= this.getHeight();        //创建面板,这样我可以使用键绑定需要的JComponent
        mainImagePanel =新PaintPanel();
        加(mainImagePanel);        / ******************************************
         *键绑定
         ****************************************** /        //主要约束AbstractAction项目
        enterAction =新EnterAction();
        escapeAction =新EscapeAction();        //获取mainImagePanel的InputMap和对的关键作用
        mainImagePanel.getInputMap()把(KeyStroke.getKeyStroke(进入)doEnterAction);
        mainImagePanel.getInputMap()把(KeyStroke.getKeyStroke(逃逸),doEscapeAction);        //此线对的AbstractAction enterAction到行动doEnterAction
        。mainImagePanel.getActionMap()把(doEnterAction,enterAction);
        。mainImagePanel.getActionMap()把(doEscapeAction,escapeAction);        / ******************************************
         *结束键绑定
         ****************************************** /
    }    //伸展并显示全屏窗口形象
    私有类PaintPanel继承JPanel
    {
        @覆盖
        公共无效的paintComponent(图形G)
        {
            如果(screenImage!= NULL)
            {
                super.paintComponent方法(G);
                g.drawImage(screenImage,0,0,宽度,高度,这一点);
            }
        }
    }    / ******************************************
     *用户输入
     ****************************************** /    私有类EnterAction扩展AbstractAction
    {
        @覆盖
        公共无效的actionPerformed(ActionEvent的五)
        {
            的System.out.println(进入了pressed。);
        }
    }    私有类EscapeAction扩展AbstractAction
    {
        @覆盖
        公共无效的actionPerformed(ActionEvent的五)
        {
            的System.out.println(计划由ESC键终止);
            System.exit(0);
        }
    }    公共静态无效的主要(字串[] args)
    {
        SwingUtilities.invokeLater(Runnable的新(){
            @覆盖
            公共无效的run()
            {
                全屏显示=新的全屏();
                show.setVisible(真);
            }
        });
    }
}

所以,下面两行导致了问题。

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


解决方案

<一个href=\"http://mail.openjdk.java.net/pipermail/macosx-port-dev/2012-November/005109.html\">http://mail.openjdk.java.net/pipermail/macosx-port-dev/2012-November/005109.html

有一种变通方法!

在你让它全屏,做 frame.setVisible(假); 然后 frame.setVisible(真) 。为什么它的工作?请教上面的链接。

EDIT 1/16/2013: The original question has been deleted. This seems to be a bug with the JDK 7 on mac OSX. I have filed a bug report with Sun (Oracle).

The file below uses the awt class GraphicsEnvironment and the method setFullScreenWindow to display an image to fullscreen. No images are included, so the screen will be gray when running the code. The key bindings should, however, still work.

There are two key bindings. Pressing 'ENTER' should print "Enter was pressed." to stdout. Pressing 'ESCAPE' should print "Program Terminated by ESC Key" to stdout and quit the program.

Using Windows 7 64 and JDK Java SE 6 AND 7 these key bindings work as expected.

Using Mac OSX 10.7 Lion and JDK Java SE 6 these key bindings work as expected.

Using Mac OSX 10.7 Lion and JDK Java SE 7 these key bindings stop working.

Rolling back to JDK Java SE 6 causes them to start working again.

I don't know if it affects other OSs.

I have tried all versions of JComponent.WHEN_IN_FOCUS etc... and none of these options fixes the problem.

Below is an SSCCE that will reproduce the error only if you are using Mac OSX 10.7 and 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);
            }
        });
    }
}

So the below two lines are causing the problem.

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

解决方案

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

There is a workaround!

After you make it fullscreen, do frame.setVisible(false); then frame.setVisible(true). Why does it work? Consult the above link.

这篇关于BUG:Java Swing的按键绑定在OSX失去功能的JDK 7 AWT setFullScreenWindow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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