2014-11-06 61 views
1

假设我有两个显示器。获取第二个显示器的x和y坐标

我想在第二个屏幕的左上角显示一个与第二个屏幕一样宽的窗口。

我知道我可以通过

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
GraphicsDevice[] gs = ge.getScreenDevices(); 

得到大小但是我不知道我将如何去获得x和第二台显示器的y坐标。任何人都可以帮忙吗?没有任何搜索帮助过我。

+0

几次如何获得/从系统TaskBar的高度+重量(提示 - 全屏) – mKorbel 2014-11-06 14:52:28

+0

_“没有搜索帮助我”_滑稽你说,x和y坐标是在javadoc的例子[GraphicsDevice](http://docs.oracle.com/javase/8/docs/api/java/awt/GraphicsDevice.html)位于页面顶部。 – icza 2014-11-06 14:54:03

+0

@icza谢谢。我会回答这个问题,并赞扬你。遗憾的是没有在文档中看到它。除非你想发布答案,然后我会接受你的答案是正确的。 – Quillion 2014-11-06 14:54:48

回答

1

没有什么复杂的,你几乎回答了你自己的问题。

下面是应该给你一个尖端的示例:

public class ScreenBoundsExample 
{ 
    public static void main (String[] args) 
    { 
     final GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice(); 
     final GraphicsDevice[] devices = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices(); 
     for (final GraphicsDevice d : devices) 
     { 
      if (d == device) 
      { 
       System.out.println ("Main screen bounds: " + d.getDefaultConfiguration().getBounds()); 
      } 
      else 
      { 
       System.out.println ("Secondary screen bounds: " + d.getDefaultConfiguration().getBounds()); 
      } 
     } 
    } 
} 

当然每个边界包含屏幕开始和(x,y)坐标。

请注意,您不能确定会有多个屏幕,因此您应该确保不会在该处出现意外异常。

+0

谢谢:)我希望icza会发布这个答案,所以我可以接受他的。但是,你已经为我回答了。我完全没有意识到边界包含了x和y坐标。 – Quillion 2014-11-06 15:19:17