【问题标题】:Full screen frame issue全屏框架问题
【发布时间】:2013-06-27 09:56:11
【问题描述】:

我有这段代码,它基本上初始化了一个新的 JFrame 并将其设置为全屏

public class FullScreenFrameTest extends JFrame {

    public FullScreenFrameTest() {
        super();
        initFrame();
        setVisible(true);

        //full screen
        GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice device = env.getDefaultScreenDevice();
        device.setFullScreenWindow(this);
        //end full screen
    }

    public void initFrame() {
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setUndecorated(true);
        setLocation(0, 0); //tried removing this, still doesn't work
        setSize(screen.width, screen.height);
    }

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
        }
        new FullScreenFrameTest();
    }
}

问题是它有时能工作,有时不能,尤其是在 Ubuntu 上:有时我会全屏看到它,有时会显示两个栏。我错过了什么?

更新

有截图:

【问题讨论】:

标签: java swing jframe fullscreen


【解决方案1】:

确保使用invokeLater() 在事件调度线程上构建您的 GUI。

更新:这是一个 SSCCE,它似乎一直有效。

import java.awt.EventQueue;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class FullScreenFrameTest extends JFrame {

    public FullScreenFrameTest() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setUndecorated(true);
        add(new JLabel("Test", JLabel.CENTER));
        GraphicsEnvironment env =
            GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice device = env.getDefaultScreenDevice();
        device.setFullScreenWindow(this);
        setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new FullScreenFrameTest();
            }
        });
    }
}

【讨论】:

  • 即使是setExtendedState()?
  • 是的,还有setExtendedState(JFrame.MAXIMIZED_BOTH)
  • FUllScreenFrameTestFullScreenFrameTest? TantaBellaRoba 不包含在invokeLater() 中?
  • 也让setVisible最后。
  • 另外,您的最后一次更新也不起作用。但是,如果我将 setVisible(true) before GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); 放置在第一次它可以工作,如果我关闭应用程序并重新打开它,它就不起作用
【解决方案2】:

改变

setLocation(0, 0); //tried removing this, still doesn't work
setSize(screen.width, screen.height);

setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
setLocationRelativeTo(null);

并在SwingUtilities.invokeLater 中调用FullScreenFrameTest() 构造函数。

更新

这可能是由于 java 运行时环境中的错误。这是报告的错误http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7057287
要了解有关此问题的更多信息,请查看HERE
更新
最后一次尝试我建议你使用JFrame#setAlwaysOnTop(true)

【讨论】:

  • 你试过我的更新了吗?您指的是屏幕右侧显示的那个栏?
  • 右边也是最上面的,我想要一个全屏未装饰的窗口,所以当我的应用程序处于全屏模式时,应该没有任何条可见
  • GraphicsDevice#isFullScreenSupported() 在您的应用程序中返回 true?
  • 我用的是jdk1.7u25,顺便说一下我没用KDE,我用的是Unity
  • 你看到我发给你的stackoverflow链接了吗?它解释了原因..在某些答案中还提到它也不适用于统一。
猜你喜欢
  • 1970-01-01
  • 2012-09-14
  • 1970-01-01
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 2013-01-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多