0

我有这段代码,它基本上初始化了一个新的 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 上:有时我会全屏看到它,有时会显示两个栏。我错过了什么?

更新

有一个截图:

截屏

4

2 回答 2

2

Make sure to build your GUI on the event dispatch thread with invokeLater().

Update: Here's an SSCCE that seems to work consistently.

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();
            }
        });
    }
}
于 2013-06-27T10:06:58.323 回答
1

改变

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

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

FullScreenFrameTest()SwingUtilities.invokeLater.

更新

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

于 2013-06-27T10:02:40.357 回答