13

我的 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

4 回答 4

22

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

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

于 2012-11-14T14:42:46.523 回答
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);

于 2009-12-21T04:41:32.970 回答
3

这不是“setFullScreenWindow”的用途。它确实适用于需要更直接访问帧缓冲区(更好的性能)的应用程序 - 例如,像 DirectX 中的 3D 游戏一样。这种暗示ONE显示器。

请参阅我所做的其他答案:JDialog Not Displaying When in Fullscreen Mode

于 2009-12-22T20:36:58.430 回答
1

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

于 2009-12-20T18:48:07.250 回答