2010-12-08 13 views

答えて

1

利用getLocation()フレームの左上の座標を取得します。あなたの画面のレイアウトと解像度に基づいて、それはどの画面にあるかを計算する必要があります。また、役に立つかもしれません何

これを使用することにより、画面の合計の寸法を得ることです:時々私は後で解決策を鑑賞(質問がされた後も、年掲載)

Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); 
+0

ありがとう、たくさんの仲間!これはそれを行います! :) – mast

0

。これは...(もともとは、Oracleのマニュアルに基づいて)同じ問題の私のより複雑なソリューション...

はお楽しみですヘルパークラスの

// First some imports (it could spare your time)… 
import java.awt.GraphicsConfiguration; 
import java.awt.GraphicsDevice; 
import java.awt.GraphicsEnvironment; 
import java.awt.Rectangle; 
import java.awt.Window; 

定義:ない方法の

/** 
* This class just stores indexes and instances of a graphics device and 
* one of its configurations (within current graphics environment). 
*/ 
public class DeviceConfig 
{ 
    public int deviceIndex = 0; 
    public int configIndex = 0; 
    public GraphicsDevice device = null; 
    public GraphicsConfiguration config = null; 
} 

定義仕事:

/** 
* This method finds the graphics device and configuration by the location 
* of specified window. 
* 
* @param window the window for which should be the identified the graphics 
*     device and configuration 
* @return instance of custom class type (DeviceConfig) that stores the 
*   indexes and instances of graphics device and configuration of 
*   current graphics environment 
*/ 
public DeviceConfig findDeviceConfig(Window window) 
{ 
    // Prepare default return value: 
    DeviceConfig deviceConfig = new DeviceConfig(); 

    // More correct would be to return null when no device or configuration 
    // has been found: 
    // 
    // DeviceConfig deviceConfig = null; 
    // 
    // See also the comments marked by *** (below). 

    Rectangle windowBounds = window.getBounds(); 
    int lastArea = 0; 

    GraphicsEnvironment graphicsEnvironment = 
     GraphicsEnvironment.getLocalGraphicsEnvironment(); 

    GraphicsDevice[] graphicsDevices = 
     graphicsEnvironment.getScreenDevices(); 

    // Search through all devices… 
    for (int i = 0; i < graphicsDevices.length; ++i) 
    { 
     GraphicsDevice graphicsDevice = graphicsDevices[i]; 

     GraphicsConfiguration[] graphicsConfigurations = 
      graphicsDevice.getConfigurations(); 

     // It is possible that your device will have only one configuration, 
     // but you cannot rely on this(!)… 
     for (int j = 0; j < graphicsConfigurations.length; ++j) 
     { 
      GraphicsConfiguration graphicsConfiguration = 
       graphicsConfigurations[j]; 

      Rectangle graphicsBounds = 
       graphicsConfiguration.getBounds(); 

      Rectangle intersection = windowBounds. 
       intersection(graphicsBounds); 

      int area = intersection.width * intersection.height; 

      if (0 != area) 
      { 
       // *** 
       // The block of code in this comments is relevant in case you 
       // want to return the null value when no device or 
       // configuration has been found. 
       /* 
       if (null == deviceConfig) 
       { 
        // In this case the better solution would be to declare 
        // the full constructor in the DeviceClass (see below) and 
        // use it here like this: 

        deviceConfig = new DeviceConfig(i, j, 
         graphicsDevice, graphicsConfiguration); 

        // (but the current solution is more simple when no 
        // constructor is defined)… 

       } 
       else 
       */ 

       if (area > lastArea) 
       { 
        lastArea = area; 
        deviceConfig.deviceIndex = i; 
        deviceConfig.configIndex = j; 
        deviceConfig.device = graphicsDevice; 
        deviceConfig.config = graphicsConfiguration; 
       } 
      } 
     } 
    } 

    return deviceConfig; 
} 

DeviceConfig Cの再定義すべての読者のための

// *** 
// The DeviceConfig class with constructors would look like this: 

public class DeviceConfig 
{ 
    public int deviceIndex; 
    public int configIndex; 
    public GraphicsDevice device; 
    public GraphicsConfiguration config; 

    /** The default constructor. (Would not be used in the second case.) */ 
    public DeviceConfig() 
    { 
     deviceIndex = 0; 
     configIndex = 0; 
     device = null; 
     config = null; 
    } 

    /** The full constructor. */ 
    public DeviceConfig(int i, int j, 
     GraphicsDevice graphicsDevice, 
     GraphicsConfiguration graphicsConfiguration) 
    { 
     deviceIndex = i; 
     configIndex = j; 
     device = graphicsDevice; 
     config = graphicsConfiguration; 
    } 
} 

ポストScriptum:(上記のコメントで述べた)null戻り値のコンテキストでの小娘あなたは、あなたのニーズに合わせてこの例を簡素化する(または事前)することができます...

(私が助けてくれることを願って... ;-))

関連する問題