macos 如何在 OSX 上用 Java 全屏显示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1155838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 06:17:07  来源:igfitidea点击:

How can I do full screen in Java on OSX

javamacosawtfullscreen

提问by Simon Andrews

I've been trying and failing to use the java full screen mode on the primary display of an OSX system. Whatever I've tried I can't seem to get rid of the 'apple' menu bar from the top of the display. I really need to paint over the entire screen. Can anyone tell me how to get rid of the menu?

我一直在尝试在 OSX 系统的主显示器上使用 java 全屏模式,但未能成功。无论我尝试过什么,我似乎都无法摆脱显示屏顶部的“苹果”菜单栏。我真的需要在整个屏幕上绘画。谁能告诉我如何摆脱菜单?

I've attached an example class which exhibits the problem - on my system the menu is still visible where I would expect to see a completely blank screen.

我附上了一个展示问题的示例类 - 在我的系统上,菜单仍然可见,我希望看到一个完全空白的屏幕。

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

回答by Michael Myers

I think your problem is here:

我认为你的问题在这里:

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

finallyblocks are always executed, so what happens here is that you window becomes full screen for a brief instant (if that) and then relinquishes the screen immediately.

finally块总是被执行,所以这里发生的事情是你的窗口在短时间内变成全屏(如果是),然后立即放弃屏幕。

Also, setVisible(true)is not necessary when you have previously called setFullScreenWindow(this), according to the Javadocs.

此外,根据JavadocssetVisible(true)当您之前调用过时,则不需要。setFullScreenWindow(this)

So I would change the constructor to 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);
    }
}

回答by Gavin S. Yancey

On OS X (10.7 and higher), it is better to use the native fullscreen mode available. You should use:

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

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

where windowis the window (JFrame, etc) that you want to take fullscreen

您想要全屏显示window的窗口(JFrame等)在哪里

回答by user667522

Thats a bit pedantic, the answer is to follow the tutorial completely, which has the essentials and is somewhat more expansive than would fit in a post. The above sample does not work because it is missing a validate();and some content. I suspect the Java Tutorial will not disappear any time soon. Below is a modified version

这有点迂腐,答案是完全按照教程进行操作,该教程具有基本知识,并且比帖子中的内容更广泛。上面的示例不起作用,因为它缺少一个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();
    }
}