如何在OSX上使用Java进行全屏显示

时间:2009-07-20 20:39:31

标签: java macos awt fullscreen

我一直在尝试并且未能在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();
    }
}

3 个答案:

答案 0 :(得分:12)

我认为你的问题在这里:

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

答案 1 :(得分:7)

在OS X(10.7及更高版本)上,最好使用可用的本机全屏模式。你应该使用:

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

其中window是您要全屏显示的窗口(JFrame等)

答案 2 :(得分:0)

这有点迂腐,答案是完全遵循教程,它具有必需品,并且比在帖子中更适合扩展。上面的示例不起作用,因为它缺少validate();和一些内容。我怀疑Java Tutorial不会很快消失。以下是修改后的版本

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