如何让 JFrame 满屏显示?

How to make JFrame fullfill all the monitor screen?

我正在 Java 中创建一个需要满足所有监视器屏幕的应用程序。

我正在使用 .setExtendedState(MAXIMIZED_BOTH) 但它只满足工作区。

        JFrame frame = new JFrame("Pomodoro");
        frame.setContentPane(new Pomodoro().panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
        frame.setExtendedState(MAXIMIZED_BOTH);
        frame.setUndecorated(true);
     }

我希望该应用程序能够占据所有监视器屏幕 - 就像 youtube 视频一样。 现在,当我打开该应用程序时,它的右侧和顶部都有我的 Linux 菜单,但是当我将我的应用程序移至第二台显示器时,它会显示所有显示器屏幕。

虽然这通常足以显示全屏 window(方法顺序很重要!):

public class FullscreenWindow
{
    public static void main ( final String[] args )
    {
        SwingUtilities.invokeLater ( new Runnable ()
        {
            @Override
            public void run ()
            {
                final JFrame frame = new JFrame ();
                frame.setExtendedState ( JFrame.MAXIMIZED_BOTH );
                frame.setUndecorated ( true );
                frame.setVisible ( true );
            }
        } );
    }
}

不足以让您的系统将 window 识别为真正的全屏。你是否需要它取决于你,因为真正的全屏 window 有它自己的优点和缺点。

要在您的系统中使任何 Window 成为真正的全屏 window - 您需要像这样将其传递到图形设备中:

public class FullscreenWindow
{
    public static void main ( final String[] args )
    {
        SwingUtilities.invokeLater ( new Runnable ()
        {
            @Override
            public void run ()
            {
                final JFrame frame = new JFrame ();
                frame.setExtendedState ( JFrame.MAXIMIZED_BOTH );
                frame.setUndecorated ( true );
                frame.setVisible ( true );
                frame.getGraphicsConfiguration ().getDevice ().setFullScreenWindow ( frame );
            }
        } );
    }
}

图形设备基本上代表您连接到 PC 的单个屏幕设备,显然可能不止一个。

虽然上面的示例只会让您的 window 在其当前图形设备上全屏显示(window 主要放置在其上的屏幕),但您可以在任何设备上使 window 全屏显示其他可用的图形设备。

您可以像这样检索可用图形设备的完整列表:

GraphicsDevice[] screenDevices = GraphicsEnvironment.getLocalGraphicsEnvironment ().getScreenDevices ();

您还可以请求图形设备的实际尺寸及其相互之间的相对位置,以更好地了解用户屏幕的相对位置(在系统设置中配置)。