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



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

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

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

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

尝试:

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

然后,您将在阵列中拥有所有显示器。

现在获取宽度和高度:

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

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

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

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

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

从中您可以获得尺寸:

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

除了注释之外,您还可以获得所有可用的监视器:

GraphicsDevice.getLocalGraphicsEnvironment().getScreenDevices()

并了解他们的决议

您可以使用

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

最新更新