Full screen frame issue

元气小坏坏 提交于 2019-12-11 21:14:51

问题


I have this code, which basically initializes a new JFrame and sets it full screen

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

The problem is that it sometimes it does work and sometimes it doesn't, especially with Ubuntu: sometimes i see it full screen, sometimes the two bars are shown. What am i missing?

UPDATE

There is a screenshot:


回答1:


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



回答2:


Change

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

To

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

And call FullScreenFrameTest() constructor within SwingUtilities.invokeLater.

UPDATE

This might be due to the bug in java run time environment. Here is the bug reported http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7057287
To know more about this issue look at HERE.
UPDATE
As last tryI would suggest you to use JFrame#setAlwaysOnTop(true)



来源:https://stackoverflow.com/questions/17340114/full-screen-frame-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!