Java如何在多个显示器上全屏显示?

14

我Java应用程序的片段:

 JFrame f = new JFrame();
 GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
 GraphicsDevice gd = ge.getDefaultScreenDevice();
 gd.setFullScreenWindow(f);

它的作用是使其自我全屏。现在奇怪的是程序只在一个显示器上全屏!例如,我有一个 Windows Vista 系统,有两个屏幕组合成一个桌面。如何让程序自动全屏到所有显示器?


好的,我尝试过那样做:

import java.awt.image.ColorModel;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;

class grdevs
{
    public static void main(String [] args)
    {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice[] gs = ge.getScreenDevices();
        for(GraphicsDevice curGs : gs)
        {
            GraphicsConfiguration[] gc = curGs.getConfigurations();
            for(GraphicsConfiguration curGc : gc)
            {
                Rectangle bounds = curGc.getBounds();
                ColorModel cm = curGc.getColorModel();

                System.out.println("" + bounds.getX() + "," + bounds.getY() + " " + bounds.getWidth() + "x" + bounds.getHeight() + " " + cm);
            }
        } 
    }
}

但它给出了:

0.0,0.0 1024.0x768.0 DirectColorModel: rmask=ff0000 gmask=ff00 bmask=ff amask=0
0.0,0.0 1024.0x768.0 DirectColorModel: rmask=ff0000 gmask=ff00 bmask=ff amask=0
0.0,0.0 1024.0x768.0 DirectColorModel: rmask=ff0000 gmask=ff00 bmask=ff amask=0
0.0,0.0 1024.0x768.0 DirectColorModel: rmask=ff0000 gmask=ff00 bmask=ff amask=0
0.0,0.0 1024.0x768.0 DirectColorModel: rmask=ff0000 gmask=ff00 bmask=ff amask=0
0.0,0.0 1024.0x768.0 DirectColorModel: rmask=ff0000 gmask=ff00 bmask=ff amask=0
1024.0,0.0 1024.0x768.0 DirectColorModel: rmask=ff0000 gmask=ff00 bmask=ff amask=0
1024.0,0.0 1024.0x768.0 DirectColorModel: rmask=ff0000 gmask=ff00 bmask=ff amask=0
1024.0,0.0 1024.0x768.0 DirectColorModel: rmask=ff0000 gmask=ff00 bmask=ff amask=0
1024.0,0.0 1024.0x768.0 DirectColorModel: rmask=ff0000 gmask=ff00 bmask=ff amask=0
1024.0,0.0 1024.0x768.0 DirectColorModel: rmask=ff0000 gmask=ff00 bmask=ff amask=0
1024.0,0.0 1024.0x768.0 DirectColorModel: rmask=ff0000 gmask=ff00 bmask=ff amask=0

例如,我会期望一个能够支持2048x768分辨率的设备,因为它们合并成了一个(我点击了“扩展桌面”)。

4个回答

26

对于Ash的代码,更一般的解决方案是合并所有图形配置的边界。

Rectangle2D result = new Rectangle2D.Double();
GraphicsEnvironment localGE = GraphicsEnvironment.getLocalGraphicsEnvironment();
for (GraphicsDevice gd : localGE.getScreenDevices()) {
  for (GraphicsConfiguration graphicsConfiguration : gd.getConfigurations()) {
    result.union(result, graphicsConfiguration.getBounds(), result);
  }
}
f.setSize(result.getWidth(), result.getHeight());

这将适用于竖直对齐的显示器以及水平对齐的显示器。


6
您可以尝试以下方法:
int width = 0;
int height = 0;
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gs = ge.getScreenDevices();
for (GraphicsDevice curGs : gs)
{
  DisplayMode mode = curGs.getDisplayMode();
  width += mode.getWidth();
  height = mode.getHeight();
}

这应该计算多个屏幕的总宽度。显然,它只支持上面所示的水平对齐屏幕 - 您需要分析图形配置的边界以处理其他监视器对齐方式(取决于您希望使其多么牢固)。

编辑:然后设置框架的大小:f.setSize(width, height);


我同意。正常情况下,一个显示器会最大化/全屏显示。如果您有扩展桌面显示器,则只能覆盖大桌面矩形内的“部分”矩形。即使它们无法覆盖所有桌面区域(例如对角线上的两个显示器),因此您必须找到大桌面矩形并手动设置大小。 - helios

3

"setFullScreenWindow"并不是为此而设的。它真正的作用是为了那些想要更直接访问帧缓冲区(更好的性能)的应用程序,比如一个使用DirectX的3D游戏。这种情况暗示只有一个监视器。

请参阅我做的另一个答案:JDialog Not Displaying When in Fullscreen Mode


1

当你在Windows系统下使用两个显示器时,最大化窗口的行为是正常的。为了获得完整的分辨率大小,你需要查看GraphicsConfiguration以检查每个GraphicsDevice。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接