2014-11-06 102 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