1

EDIT 1/16/2013: 元の質問は削除されました。これは、Mac OSX 上の JDK 7 のバグのようです。Sun (Oracle) にバグレポートを提出しました。

以下のファイルは、awt クラスの GraphicsEnvironment とメソッド setFullScreenWindow を使用して、画像を全画面表示します。画像が含まれていないため、コードを実行すると画面が灰色になります。ただし、キー バインドは引き続き機能するはずです。

2 つのキー バインドがあります。「ENTER」を押すと、「Enter が押されました」と出力されます。標準出力へ。「ESCAPE」を押すと、「Program Terminated by ESC Key」が標準出力に出力され、プログラムが終了します。

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 にロールバックすると、再び機能し始めます。

他のOSに影響するかどうかはわかりません。

JComponent.WHEN_IN_FOCUS などのすべてのバージョンを試しましたが、これらのオプションのどれも問題を解決しません。

以下は、Mac OSX 10.7 および JDK Java SE 7 を使用している場合にのみエラーを再現するSSCCEです。

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);
            }
        });
    }
}

したがって、以下の 2 行が問題を引き起こしています。

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

3 に答える 3

5

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

回避策があります!

フルスクリーンにしたframe.setVisible(false);後、 を実行しframe.setVisible(true)ます。なぜそれが機能するのですか?上記のリンクを参照してください。

于 2013-11-03T09:33:20.203 に答える
3

hereに記載されているように、一部の static final 変数は、依存クラスで効果的にインライン化される場合があります。KeyStrokeaを a にString、それStringを anに暗黙的に接続するキー バインドは、が別のクラスで静的に定義されActionている場合、この動作を示すことがあります。ここStringで提案されている簡単な手段の 1 つは、完全なビルドを行うことです。これで問題が解決した場合は、逆方向に作業して依存関係を軽減できる場合があります。

于 2013-01-14T12:27:16.363 に答える
2

このコードでキー バインディングが機能する Mac システムに、jdk-7u11-macosx-x64.dmg をインストールしました。インストール後、キーバインドが機能しなくなります。現時点では、これは OSX 向けの新しいバージョンの JDK のバグであり、報告する予定であると確信しています。

これを解決するのを手伝ってくれてありがとう、しかしコードは問題ないことがわかりました。

于 2013-01-16T20:32:39.153 に答える