簡體   English   中英

如何在Java中獲取當前監視器的寬度和高度

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

如何在Java中的多監視器方案中獲取當前監視器的屏幕寬度和高度。

GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
    int width = gd.getDisplayMode().getWidth();
    int height = gd.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()

並知道他們的決議

嘗試:

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

然后,將所有監視器放在陣列中。

現在獲取寬度和高度:

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

您可以使用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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM