java & fullscreen over multiple monitors

混江龙づ霸主 提交于 2019-11-28 20:47:45
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);

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

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.

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.

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

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 GraphicsConfiguration to check each GraphicsDevice.

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

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