Java Toolkit 获取第二个屏幕尺寸
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6322627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Toolkit Getting Second screen size
提问by Humphrey
I have two screens plugged into my computer and was wondering if there was a way in JFrame or Toolkit of detecting which screen the window is on?
我有两个屏幕插入我的计算机,想知道 JFrame 或 Toolkit 中是否有办法检测窗口所在的屏幕?
I have this code:
我有这个代码:
java.awt.Toolkit.getDefaultToolkit().getScreenSize();
Which gets my screen size of my main screen, but how can I get the size of my second screen, or detect which screen the window is on?
哪个获取我的主屏幕的屏幕大小,但是我如何获取第二个屏幕的大小,或者检测窗口在哪个屏幕上?
回答by Mat
You should take a look at GraphicsEnvironment.
你应该看看GraphicsEnvironment。
In particular, getScreenDevices()
:
特别是getScreenDevices()
:
Returns an array of all of the screen GraphicsDevice objects.
返回所有屏幕 GraphicsDevice 对象的数组。
You can get the dimensions from those GraphicDeviceobjects (indirectly, via getDisplayMode
). (That page also shows how to put a frame on a specific device.)
您可以从这些GraphicDevice对象(间接地,通过getDisplayMode
)获取尺寸。(该页面还显示了如何在特定设备上放置框架。)
And you can get from a JFrame to its device via the getGraphicsConfigration()
method, which returns a GraphicsConfigurationthat has a getDevice()
. (The getIDstring()
method will probably enable you to differentiate between the screens.)
而且你可以从通过一个JFrame其设备getGraphicsConfigration()
的方法,它返回一个GraphicsConfiguration的,有一个getDevice()
。(该getIDstring()
方法可能使您能够区分屏幕。)
回答by Martijn Courteaux
Check out this thread on StackOverflow. The code in from the OP uses this code:
在 StackOverflow 上查看此线程。来自 OP 的代码使用以下代码:
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gs = ge.getScreenDevices();
for(GraphicsDevice curGs : gs)
{
GraphicsConfiguration[] gc = curGs.getConfigurations();
for(GraphicsConfiguration curGc : gc)
{
Rectangle bounds = curGc.getBounds();
System.out.println(bounds.getX() + "," + bounds.getY() + " " + bounds.getWidth() + "x" + bounds.getHeight());
}
}
The output was:
输出是:
0.0,0.0 1024.0x768.0
0.0,0.0 1024.0x768.0
0.0,0.0 1024.0x768.0
0.0,0.0 1024.0x768.0
0.0,0.0 1024.0x768.0
0.0,0.0 1024.0x768.0
1024.0,0.0 1024.0x768.0
1024.0,0.0 1024.0x768.0
1024.0,0.0 1024.0x768.0
1024.0,0.0 1024.0x768.0
1024.0,0.0 1024.0x768.0
1024.0,0.0 1024.0x768.0
So, you can see it returns two screens. He had two screens of 1024x768, positioned next to each other. The code can be optimized to, since you only want width and height:
所以,你可以看到它返回两个屏幕。他有两个 1024x768 的屏幕,并排放置。代码可以优化为,因为您只需要宽度和高度:
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gs = ge.getScreenDevices();
for(GraphicsDevice curGs : gs)
{
DisplayMode dm = curGs.getDisplayMode();
System.out.println(dm.getWidth() + " x " + dm.getHeight());
}
回答by Femi
If you use the code shown hereyou can iterate over all the GraphicsDevice
s in the system and get their dimensions. Given that you can create a JFrame on a specific GraphicsDevice you can also fetch the specific GraphicsDevice a JFrame is on by getting the JFrame's Window, calling http://download.oracle.com/javase/6/docs/api/java/awt/Window.html#getGraphicsConfiguration() on the Window and then calling getGraphicsDevice
on that.
如果您使用此处显示的代码,您可以遍历GraphicsDevice
系统中的所有s 并获取它们的维度。鉴于您可以在特定的 GraphicsDevice 上创建 JFrame,您还可以通过获取 JFrame 的窗口来获取 JFrame 所在的特定 GraphicsDevice,调用http://download.oracle.com/javase/6/docs/api/java/awt /Window.html#getGraphicsConfiguration() 在 Window 上然后调用getGraphicsDevice
它。
回答by Alpha2k
Straight to the code, try this :)
直接看代码,试试这个:)
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gs = ge.getScreenDevices();
for (int i = 0; i < gs.length; i++) {
System.out.println(gs[i].getDisplayMode().getWidth()+" "+gs[i].getDisplayMode().getHeight());
//System.out.println(java.awt.Toolkit.getDefaultToolkit().getScreenSize());
// to check default resolution of the device
}