java 和全屏显示在多个显示器上

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1936547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 18:35:56  来源:igfitidea点击:

java & fullscreen over multiple monitors

javaswingfullscreen

提问by Folkert van Heusden

A snippet from my Java application:

我的 Java 应用程序中的一个片段:

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

So what it does is make it self fullscreen. Now the odd thing is that the program is fullscreen but only on one monitor! E.g. I have a windows vista system with two screens that are combined in one desktop. What to do automatically let it go fullscreen over all monitors?

所以它所做的是让它自己全屏。现在奇怪的是程序是全屏的,但只在一台显示器上!例如,我有一个 Windows Vista 系统,其中两个屏幕组合在一个桌面上。如何自动让它在所有显示器上全屏显示?



Ok I tried that:

好的,我试过了:

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

but it gives:

但它给出了:

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

E.g I would expect a device capable of 2048x768 as they are combined in one (I clicked on "extend desktop").

例如,我希望设备能够达到 2048x768,因为它们合二为一(我点击了“扩展桌面”)。

采纳答案by Ash

You could try:

你可以试试:

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

This should calculate the total width of multiple screens. Obviously it only supports horizontally aligned screens in the form above - you'd have to analyse the bounds of the graphics configurations to handle other monitor alignments (depends how bulletproof you want to make it).

这应该计算多个屏幕的总宽度。显然,它仅支持上述形式的水平对齐屏幕 - 您必须分析图形配置的边界以处理其他显示器对齐方式(取决于您想要使其防弹的程度)。

Edit: And then set the size of your frame: f.setSize(width, height);

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

回答by tiestvilee

A more general solution to Ash's code is to union the bounds of all the graphics configurations

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

This will work for vertically aligned monitors as well as horizontal.

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

回答by M1EK

This is not what "setFullScreenWindow" is for. It's really for applications that want more direct access to the framebuffer (better performance) - like a 3D game does in DirectX, for instance. This kind of implies ONE monitor.

这不是“setFullScreenWindow”的用途。它确实适用于希望更直接地访问帧缓冲区(更好的性能)的应用程序——例如 DirectX 中的 3D 游戏。这种意味着一个显示器。

See this other answer I did: JDialog Not Displaying When in Fullscreen Mode

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

回答by unholysampler

That is the normal behavior when you maximize a window in Windows when you have two monitors. In order two get the full resolution size, you will need to look at GraphicsConfigurationto check each GraphicsDevice.

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

回答by Carles Barrobés

Using java.awt.Toolkit you can get the full screen size (all monitors):

使用 java.awt.Toolkit 您可以获得全屏尺寸(所有显示器):

Toolkit tk = Toolkit.getDefaultToolkit();
Dimension dim = tk.getScreenSize();