你如何在java中获得屏幕宽度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1936566/
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
How do you get the screen width in java?
提问by Andrew
Does anyone know how you would get the screen width in java? I read something about some toolkit method but I'm not quite sure what that is.
有谁知道你将如何在java中获得屏幕宽度?我阅读了一些关于工具包方法的内容,但我不太确定那是什么。
Thanks, Andrew
谢谢,安德鲁
采纳答案by Ben McCann
java.awt.Toolkit.getDefaultToolkit().getScreenSize()
回答by tangens
The following code should do it (haven't tried it):
下面的代码应该可以做到(没试过):
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
gd.getDefaultConfiguration().getBounds().getWidth();
edit:
编辑:
For multiple monitors you should use the following code (taken from the javadoc of java.awt.GraphicsConfiguration
:
对于多个监视器,您应该使用以下代码(取自 的javadocjava.awt.GraphicsConfiguration
:
Rectangle virtualBounds = new Rectangle();
GraphicsEnvironment ge = GraphicsEnvironment.
getLocalGraphicsEnvironment();
GraphicsDevice[] gs =
ge.getScreenDevices();
for (int j = 0; j < gs.length; j++) {
GraphicsDevice gd = gs[j];
GraphicsConfiguration[] gc =
gd.getConfigurations();
for (int i=0; i < gc.length; i++) {
virtualBounds =
virtualBounds.union(gc[i].getBounds());
}
}
回答by unholysampler
You can get it by using the AWT Toolkit.
您可以使用AWT Toolkit获得它。
回答by Dan Dyer
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
回答by jmoreno
The OP probably wanted something like this:
OP 可能想要这样的东西:
Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
回答by Alex Ntousias
Toolkit.getDefaultToolkit().getScreenSize().getWidth()
回答by Michael Brewer-Davis
Toolkithas a number of classes that would help:
Toolkit有许多类可以提供帮助:
- getScreenSize- raw screen size
- getScreenInsets- gets size of toolbar, dock
- getScreenResolution- dpi
- getScreenSize- 原始屏幕大小
- getScreenInsets- 获取工具栏、停靠栏的大小
- getScreenResolution- dpi
We end up using 1 and 2, to compute usable maximum window size. To get the relevant GraphicsConfiguration, we use
我们最终使用 1 和 2 来计算可用的最大窗口大小。要获取相关的 GraphicsConfiguration,我们使用
GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0].getDefaultConfiguration();
but there may be smarter multiple-monitor solutions.
但可能有更智能的多显示器解决方案。
回答by Lawrence Dol
Here are the two methods I use, which account for multiple monitors and task-bar insets. If you don't need the two methods separately, you can, of course, avoid getting the graphics config twice.
这是我使用的两种方法,它们考虑了多个监视器和任务栏插入。如果您不需要分别使用这两种方法,当然可以避免两次获取图形配置。
static public Rectangle getScreenBounds(Window wnd) {
Rectangle sb;
Insets si=getScreenInsets(wnd);
if(wnd==null) {
sb=GraphicsEnvironment
.getLocalGraphicsEnvironment()
.getDefaultScreenDevice()
.getDefaultConfiguration()
.getBounds();
}
else {
sb=wnd
.getGraphicsConfiguration()
.getBounds();
}
sb.x +=si.left;
sb.y +=si.top;
sb.width -=si.left+si.right;
sb.height-=si.top+si.bottom;
return sb;
}
static public Insets getScreenInsets(Window wnd) {
Insets si;
if(wnd==null) {
si=Toolkit.getDefaultToolkit().getScreenInsets(GraphicsEnvironment
.getLocalGraphicsEnvironment()
.getDefaultScreenDevice()
.getDefaultConfiguration());
}
else {
si=wnd.getToolkit().getScreenInsets(wnd.getGraphicsConfiguration());
}
return si;
}
回答by Pacerier
The working area is the desktop area of the display, excluding taskbars, docked windows, and docked tool bars.
工作区是显示器的桌面区域,不包括任务栏、停靠窗口和停靠工具栏。
If what you want is the "working area" of the screen, use this:
如果您想要的是屏幕的“工作区”,请使用以下命令:
public static int GetScreenWorkingWidth() {
return java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().width;
}
public static int GetScreenWorkingHeight() {
return java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().height;
}
回答by jan
If you need the resolution of the screen that a certain component is currently assigned to (something like most part of the root window is visible on that screen), you can use this answer.
如果您需要某个组件当前分配到的屏幕分辨率(例如根窗口的大部分在该屏幕上可见),您可以使用此答案。