11

OSXシステムのプライマリディスプレイでJavaフルスクリーンモードを使用しようとして失敗しました。私が試したものが何であれ、ディスプレイの上部から「アップル」メニューバーを取り除くことができないようです。私は本当に画面全体にペイントする必要があります。メニューを取り除く方法を教えてもらえますか?

問題を示すサンプルクラスを添付しました。私のシステムでは、完全に空白の画面が表示されるはずのメニューがまだ表示されています。

import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;

public class FullScreenFrame extends JFrame implements KeyListener {

    public FullScreenFrame () {
        addKeyListener(this);
        setUndecorated(true);
        GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();

        if (gd.isFullScreenSupported()) {
            try {
                gd.setFullScreenWindow(this);
            }
            finally {
                gd.setFullScreenWindow(null);
            }
        }
        else {
            System.err.println("Full screen not supported");
        }

        setVisible(true);
    }

    public void keyTyped(KeyEvent e) {}
    public void keyPressed(KeyEvent e) {}
    public void keyReleased(KeyEvent e) {
        setVisible(false);
        dispose();
    }

    public static void main (String [] args) {
        new FullScreenFrame();
    }
}
4

3 に答える 3

13

私はあなたの問題がここにあると思います:

try {
        gd.setFullScreenWindow(this);
}
finally {
        gd.setFullScreenWindow(null);
}

finallyブロックは常に実行されるため、ここで発生するのは、ウィンドウが短時間(その場合)フルスクリーンになり、すぐに画面を放棄することです。

また、Javadocsによると、setVisible(true)以前に呼び出したことがある場合は必要ありません。setFullScreenWindow(this)

したがって、コンストラクターを次のように変更します。

public FullScreenFrame() {
    addKeyListener(this);

    GraphicsDevice gd =
            GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();

    if (gd.isFullScreenSupported()) {
        setUndecorated(true);
        gd.setFullScreenWindow(this);
    } else {
        System.err.println("Full screen not supported");
        setSize(100, 100); // just something to let you see the window
        setVisible(true);
    }
}
于 2009-07-20T21:05:26.663 に答える
12

OS X(10.7以降)では、使用可能なネイティブフルスクリーンモードを使用することをお勧めします。次を使用する必要があります。

com.apple.eawt.FullScreenUtilities.setWindowCanFullScreen(window,true);
com.apple.eawt.Application.getApplication().requestToggleFullScreen(window);

フルスクリーンで撮りたいwindowウィンドウ(など)はどこですかJFrame

于 2013-11-15T03:57:07.767 に答える
0

それは少し衒学的です、答えはチュートリアルに完全に従うことです。それは本質を持っていて、投稿に収まるよりいくらか広大です。validate();上記のサンプルは、および一部のコンテンツが欠落しているため、機能しません。Javaチュートリアルがすぐに消えることはないと思います。以下は修正版です

package test;

import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class FullScreenFrame extends JFrame implements KeyListener {

    public FullScreenFrame () {
        addKeyListener(this);
        setUndecorated(true);
        GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();

    if (gd.isFullScreenSupported()) {
        try {
            this.getContentPane().addKeyListener(this);
            this.getContentPane().setLayout(new BorderLayout());
            this.getContentPane().add("Center", new JLabel("Full Screen, back to normal in 10 seconds"));
            gd.setFullScreenWindow(this);
            validate();
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } finally {
            gd.setFullScreenWindow(null);
        }
    } else {
        System.err.println("Full screen not supported");
    }

}

public void keyTyped(KeyEvent e) {
    System.out.println("keyTyped:" +  e.getKeyChar() + "source:"  + e.getSource() );
}
public void keyPressed(KeyEvent e) {
    System.out.println("keyPressed:" + e.getKeyChar() + "source:"  + e.getSource() );
}
public void keyReleased(KeyEvent e) {
    System.out.println("keyReleased:" + e.getKeyChar() + "source:"  + e.getSource() );
       setVisible(false);
        dispose();
    }

    public static void main (String [] args) {
        new FullScreenFrame();
    }
}
于 2018-05-07T18:20:29.953 に答える