【问题标题】:How to get the width and height of current monitor in java如何在java中获取当前监视器的宽度和高度
【发布时间】:2014-12-11 09:29:52
【问题描述】:

如何在java中的多显示器场景中获取当前显示器的屏幕宽度和高度。

GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
    int width = gd.getDisplayMode().getWidth();
    int height = gd.getDisplayMode().getHeight();

即使我在其他显示器上运行,这段代码也给出了同一显示器的宽度和高度。

如何获取当前显示器的宽度和高度。请帮忙。

【问题讨论】:

  • 嗨 MuGiK,谢谢.. 我需要当前正在使用的显示器的分辨率。它给了我默认设备,而不是我相信的当前设备。请帮助我确定当前使用的显示器的大小,而不是默认显示器。 GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();

标签: java swing screen-size multiple-monitors


【解决方案1】:

试试:

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

然后你的所有显示器都在阵列中。

现在获取宽度和高度:

int width = gd[n].getDisplayMode().getWidth();
int height = gd[n].getDisplayMode().getHeight();

【讨论】:

  • 你如何识别当前显示器.. MuGik.
  • 'n' 是显示器的索引
  • 谢谢.. MuGik。我会重新安排我的问题。我需要找到当前正在使用的显示器的大小。
【解决方案2】:

这取决于当前屏幕的含义。如果是鼠标光标所在的屏幕,可以通过以下方式获取设备:

GraphicsDevice gd = MouseInfo.getPointerInfo().getDevice();

另一方面,如果您需要窗口所在的设备,请使用:

GraphicsDevice gd = frame.getGraphicsConfiguration().getDevice();

从这些你可以得到尺寸:

int width = gd.getDisplayMode().getWidth();
int height = gd.getDisplayMode().getHeight();

【讨论】:

    【解决方案3】:

    除了评论,您还可以获得所有可用的监视器:

    GraphicsDevice.getLocalGraphicsEnvironment().getScreenDevices()
    

    并了解他们的解决方案

    【讨论】:

    【解决方案4】:

    您可以使用Toolkit 获取屏幕尺寸,如下所示:

    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    double width = screenSize.getWidth();
    double height = screenSize.getHeight();
    

    对于多屏请尝试以下操作:

    GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
    int width = gd.getDisplayMode().getWidth();
    int height = gd.getDisplayMode().getHeight();
    

    【讨论】:

      猜你喜欢
      • 2020-03-04
      • 1970-01-01
      • 1970-01-01
      • 2011-12-08
      • 1970-01-01
      • 2023-03-04
      • 1970-01-01
      • 2021-05-29
      • 1970-01-01
      相关资源
      最近更新 更多