0
我似乎很难在Java中获得多个监视器的大小;获取辅助监视器的大小
因此,我做了一个小窗口,应该显示一个屏幕的尺寸,它适用于我的主监视器,现在我想能够确定它的监视器的大小,我用getLocation()
知道在哪里我的JFrame是,但我不知道如何获得该显示器的大小,我只能得到主显示器的大小,甚至是他们的总大小。
我似乎很难在Java中获得多个监视器的大小;获取辅助监视器的大小
因此,我做了一个小窗口,应该显示一个屏幕的尺寸,它适用于我的主监视器,现在我想能够确定它的监视器的大小,我用getLocation()
知道在哪里我的JFrame是,但我不知道如何获得该显示器的大小,我只能得到主显示器的大小,甚至是他们的总大小。
您需要进入GraphicsEnvironment
,这将使您可以访问系统上可用的所有GraphicsDevice
。
本质上讲,你需要通过每个GraphicsDevice
和测试循环,看看窗外是一个给定的GraphicsDevice
有趣的部分的范围内,则如果窗口在多个屏幕上跨越做什么。 ..
public static GraphicsDevice getGraphicsDevice(Component comp) {
GraphicsDevice device = null;
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice lstGDs[] = ge.getScreenDevices();
ArrayList<GraphicsDevice> lstDevices = new ArrayList<GraphicsDevice>(lstGDs.length);
if (comp != null && comp.isVisible()) {
Rectangle parentBounds = comp.getBounds();
/*
* If the component is not a window, we need to find its location on the
* screen...
*/
if (!(comp instanceof Window)) {
Point p = new Point(0, 0);
SwingUtilities.convertPointToScreen(p, comp);
parentBounds.setLocation(p);
}
// Get all the devices which the window intersects (ie the window might expand across multiple screens)
for (GraphicsDevice gd : lstGDs) {
GraphicsConfiguration gc = gd.getDefaultConfiguration();
Rectangle screenBounds = gc.getBounds();
if (screenBounds.intersects(parentBounds)) {
lstDevices.add(gd);
}
}
// If there is only one device listed, return it...
// Otherwise, if there is more then one device, find the device
// which the window is "mostly" on
if (lstDevices.size() == 1) {
device = lstDevices.get(0);
} else if (lstDevices.size() > 1) {
GraphicsDevice gdMost = null;
float maxArea = 0;
for (GraphicsDevice gd : lstDevices) {
int width = 0;
int height = 0;
GraphicsConfiguration gc = gd.getDefaultConfiguration();
Rectangle bounds = gc.getBounds();
Rectangle2D intBounds = bounds.createIntersection(parentBounds);
float perArea = (float) ((intBounds.getWidth() * intBounds.getHeight())/(parentBounds.width * parentBounds.height));
if (perArea > maxArea) {
maxArea = perArea;
gdMost = gd;
}
}
if (gdMost != null) {
device = gdMost;
}
}
}
return device;
}
请在发布问题之前养成堆栈溢出的现有答案。你之前询问过的问题很有可能出现在你面前。 – MarsAtomic