简体   繁体   English

如何在Java中获取当前监视器的宽度和高度

[英]How to get the width and height of current monitor in java

how to get width and height of screen of current monitor in multimonitor scenarios in java. 如何在Java中的多监视器方案中获取当前监视器的屏幕宽度和高度。

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

This piece of code gives the width and height of same monitor even if I run in other monitor. 即使我在其他显示器上运行,这段代码也给出了同一显示器的宽度和高度。

How do you get the width and height of current monitor. 如何获得当前监视器的宽度和高度。 Please help. 请帮忙。

It depends on what you mean by the current screen. 这取决于您当前屏幕的含义。 If it is the screen where the mouse cursor is, you can get the device with: 如果是鼠标光标所在的屏幕,则可以通过以下方式获取设备:

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

On the other hand, if you need the device where a window is located, use: 另一方面,如果您需要窗口所在的设备,请使用:

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

From these you can obtain the dimensions: 从这些中,您可以获取尺寸:

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

In addition to the comment, you may obtain all available monitors: 除了评论之外,您还可以获得所有可用的监视器:

GraphicsDevice.getLocalGraphicsEnvironment().getScreenDevices()

and know them resolutions 并知道他们的决议

Try: 尝试:

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

Then you'll have all your monitor in the array. 然后,将所有监视器放在阵列中。

Now get width and height: 现在获取宽度和高度:

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

You can get screen dimensions using Toolkit as follows: 您可以使用Toolkit获得屏幕尺寸,如下所示:

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

For multi-screen try following: 对于多屏幕,请尝试以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM