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