检测当前屏幕边界

我正在开发一个具有setDecoration(false)的应用程序,我有一个MouseMotionlistener所以我可以移动它,此刻我正在尝试制作一个最大化按钮。 在默认监视器上它可以很好地工作,但在第二个监视器上,如果我单击最大化按钮,它将最大化到默认屏幕。 如何获取应用程序当前所在屏幕的X和Y坐标?

IE我在1600×900都有2个显示器,所以如果应用程序在监视器1上,则X和Y将为0和0,但如果是第二个监视器则为1600和0。

但我需要它,所以它适用于所有尺寸的显示器,即1200×800,或者如果显示器垂直而不是水平。

答案取决于屏幕的定义。 您想要默认的屏幕边界还是特定的屏幕边界?

我使用以下(及其变体)来确定单个屏幕的屏幕边界

 public static GraphicsDevice getGraphicsDeviceAt(Point pos) { GraphicsDevice device = null; GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice lstGDs[] = ge.getScreenDevices(); ArrayList lstDevices = new ArrayList(lstGDs.length); for (GraphicsDevice gd : lstGDs) { GraphicsConfiguration gc = gd.getDefaultConfiguration(); Rectangle screenBounds = gc.getBounds(); if (screenBounds.contains(pos)) { lstDevices.add(gd); } } if (lstDevices.size() == 1) { device = lstDevices.get(0); } return device; } public static Rectangle getScreenBoundsAt(Point pos) { GraphicsDevice gd = getGraphicsDeviceAt(pos); Rectangle bounds = null; if (gd != null) { bounds = gd.getDefaultConfiguration().getBounds(); } return bounds; } 

基本思想是提供屏幕位置并找到与其匹配的屏幕。 我有一个ComponentWindow变化,但基本上,它归结为这个。

MouseEvent ,您可以通过调用MouseEvent.getLocationOnScreen来获得足够的屏幕坐标

现在,根据你的问题,听起来你想知道整个“虚拟”屏幕边界(我可能是错的),但我使用这种方法(实际上我用它来动态创建多显示器壁纸,但那是另一个题)

 public static Rectangle getVirtualScreenBounds() { GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice lstGDs[] = ge.getScreenDevices(); Rectangle bounds = new Rectangle(); for (GraphicsDevice gd : lstGDs) { bounds.add(gd.getDefaultConfiguration().getBounds()); } return bounds; } 

基本上,它只是遍历所有屏幕设备并将那些区域添加在一起以形成“虚拟”矩形。 您可以使用相同的概念将每个屏幕设备的边界作为数组返回。

您可以使用它来获取屏幕尺寸。

 Toolkit toolkit = Toolkit.getDefaultToolkit (); Dimension dim = toolkit.getScreenSize(); System.out.println("Width of Screen Size is "+dim.width+" pixels"); System.out.println("Height of Screen Size is "+dim.height+" pixels");