回答
您可以使用Toolkit.getScreenSize()
方法获得屏幕尺寸。
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
double width = screenSize.getWidth();
double height = screenSize.getHeight();
在多显示器配置,你应该这样做:
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
int width = gd.getDisplayMode().getWidth();
int height = gd.getDisplayMode().getHeight();
如果你想在DPI屏幕分辨率,你将不得不使用的方法getScreenResolution()
在Toolkit
。
资源:
这对我不起作用。我有一个3840x2160监视器,但getScreenSize返回1920x1080。 – ZhekaKozlov 2018-03-01 10:25:32
这个电话会给你你想要的信息。
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int resolution =Toolkit.getDefaultToolkit().getScreenResolution();
System.out.println(resolution);
此代码将枚举系统上的图形装置(如果安装了多个监视器),和你可以使用这些信息确定显示器亲和力或自动放置(某些系统使用侧面监视器进行实时显示,而应用程序在后台运行,并且此类显示器可通过大小,屏幕颜色等进行标识):
// Test if each monitor will support my app's window
// Iterate through each monitor and see what size each is
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gs = ge.getScreenDevices();
Dimension mySize = new Dimension(myWidth, myHeight);
Dimension maxSize = new Dimension(minRequiredWidth, minRequiredHeight);
for (int i = 0; i < gs.length; i++)
{
DisplayMode dm = gs[i].getDisplayMode();
if (dm.getWidth() > maxSize.getWidth() && dm.getHeight() > maxSize.getHeight())
{ // Update the max size found on this monitor
maxSize.setSize(dm.getWidth(), dm.getHeight());
}
// Do test if it will work here
}
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
double width = screenSize.getWidth();
double height = screenSize.getHeight();
framemain.setSize((int)width,(int)height);
framemain.setResizable(true);
framemain.setExtendedState(JFrame.MAXIMIZED_BOTH);
这是给定组件当前分配的屏幕分辨率(类似于根窗口的大部分部分在该屏幕上可见)。
public Rectangle getCurrentScreenBounds(Component component) {
return component.getGraphicsConfiguration().getBounds();
}
用法:
Rectangle currentScreen = getCurrentScreenBounds(frameOrWhateverComponent);
int currentScreenWidth = currentScreen.width // current screen width
int currentScreenHeight = currentScreen.height // current screen height
// absolute coordinate of current screen > 0 if left of this screen are further screens
int xOfCurrentScreen = currentScreen.x
如果你要尊重工具栏等你需要用这个来计算,得:
GraphicsConfiguration gc = component.getGraphicsConfiguration();
Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(gc);
这里的一些功能代码(Java的8 ),它返回最右边屏幕最右边的x位置。如果未找到屏幕,则返回0.
GraphicsDevice devices[];
devices = GraphicsEnvironment.
getLocalGraphicsEnvironment().
getScreenDevices();
return Stream.
of(devices).
map(GraphicsDevice::getDefaultConfiguration).
map(GraphicsConfiguration::getBounds).
mapToInt(bounds -> bounds.x + bounds.width).
max().
orElse(0);
以下是指向JavaDoc的链接。
GraphicsEnvironment.getLocalGraphicsEnvironment()
GraphicsEnvironment.getScreenDevices()
GraphicsDevice.getDefaultConfiguration()
GraphicsConfiguration.getBounds()
这三个函数在Java中返回的屏幕尺寸。该代码考虑了多监视器设置和任务栏。包括的功能有:getScreenInsets(),getScreenWorkingArea()和getScreenTotalArea()。
代码:
/**
* getScreenInsets, This returns the insets of the screen, which are defined by any task bars
* that have been set up by the user. This function accounts for multi-monitor setups. If a
* window is supplied, then the the monitor that contains the window will be used. If a window
* is not supplied, then the primary monitor will be used.
*/
static public Insets getScreenInsets(Window windowOrNull) {
Insets insets;
if (windowOrNull == null) {
insets = Toolkit.getDefaultToolkit().getScreenInsets(GraphicsEnvironment
.getLocalGraphicsEnvironment().getDefaultScreenDevice()
.getDefaultConfiguration());
} else {
insets = windowOrNull.getToolkit().getScreenInsets(
windowOrNull.getGraphicsConfiguration());
}
return insets;
}
/**
* getScreenWorkingArea, This returns the working area of the screen. (The working area excludes
* any task bars.) This function accounts for multi-monitor setups. If a window is supplied,
* then the the monitor that contains the window will be used. If a window is not supplied, then
* the primary monitor will be used.
*/
static public Rectangle getScreenWorkingArea(Window windowOrNull) {
Insets insets;
Rectangle bounds;
if (windowOrNull == null) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
insets = Toolkit.getDefaultToolkit().getScreenInsets(ge.getDefaultScreenDevice()
.getDefaultConfiguration());
bounds = ge.getDefaultScreenDevice().getDefaultConfiguration().getBounds();
} else {
GraphicsConfiguration gc = windowOrNull.getGraphicsConfiguration();
insets = windowOrNull.getToolkit().getScreenInsets(gc);
bounds = gc.getBounds();
}
bounds.x += insets.left;
bounds.y += insets.top;
bounds.width -= (insets.left + insets.right);
bounds.height -= (insets.top + insets.bottom);
return bounds;
}
/**
* getScreenTotalArea, This returns the total area of the screen. (The total area includes any
* task bars.) This function accounts for multi-monitor setups. If a window is supplied, then
* the the monitor that contains the window will be used. If a window is not supplied, then the
* primary monitor will be used.
*/
static public Rectangle getScreenTotalArea(Window windowOrNull) {
Rectangle bounds;
if (windowOrNull == null) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
bounds = ge.getDefaultScreenDevice().getDefaultConfiguration().getBounds();
} else {
GraphicsConfiguration gc = windowOrNull.getGraphicsConfiguration();
bounds = gc.getBounds();
}
return bounds;
}
int screenResolution = Toolkit.getDefaultToolkit().getScreenResolution();
System.out.println(""+screenResolution);
欢迎来到Stack Overflow!尽管这段代码可以解决这个问题,但[包括一个解释](// meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers)确实有助于提高您的帖子的质量。请记住,您将来会为读者回答问题,而这些人可能不知道您的代码建议的原因。也请尽量不要用解释性注释来挤占代码,这会降低代码和解释的可读性! – kayess 2016-12-20 14:43:12
下面是一个代码片段,我经常使用。它会返回完整的可用屏幕区域(即使在多显示器设置中),同时保留原生监视器位置。
public static Rectangle getMaximumScreenBounds() {
int minx=0, miny=0, maxx=0, maxy=0;
GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
for(GraphicsDevice device : environment.getScreenDevices()){
Rectangle bounds = device.getDefaultConfiguration().getBounds();
minx = Math.min(minx, bounds.x);
miny = Math.min(miny, bounds.y);
maxx = Math.max(maxx, bounds.x+bounds.width);
maxy = Math.max(maxy, bounds.y+bounds.height);
}
return new Rectangle(minx, miny, maxx-minx, maxy-miny);
}
在有两个全高清显示器,其中左侧是设置为主显示器(在Windows中设置)的计算机,该函数返回
java.awt.Rectangle[x=0,y=0,width=3840,height=1080]
在相同的设置,但将正确的显示器设置为主显示器,功能返回
java.awt.Rectangle[x=-1920,y=0,width=3840,height=1080]
- 1. 如何获得屏幕分辨率R
- 2. 如何在java中获得屏幕分辨率?
- 3. 获取屏幕分辨率
- 4. 获取屏幕分辨率
- 5. 如何在C++中获得屏幕分辨率?
- 6. 如何在ActionScript 3中获得用户的屏幕分辨率?
- 7. 在iphone中获取屏幕分辨率
- 8. 屏幕分辨率
- 9. 屏幕分辨率
- 10. 屏幕分辨率
- 11. 屏幕分辨率
- 12. 屏幕分辨率
- 13. 屏幕分辨率
- 14. 如何获得wpf中的实际桌面屏幕分辨率?
- 15. iPhone模拟器屏幕分辨率vs iPhone屏幕分辨率
- 16. 如何在所有屏幕分辨率
- 17. 如何获得显示分辨率(屏幕尺寸)?
- 18. 如何获得设备(Windows Phone)的屏幕分辨率
- 19. 如何获得用户的屏幕分辨率与PHP
- 20. 如何获得黑莓屏幕分辨率?
- 21. 如何获得Android设备的屏幕分辨率..?
- 22. 如何从hWnd获得显示器屏幕分辨率?
- 23. 在XNA中获得支持的屏幕分辨率?
- 24. 如何阅读屏幕分辨率 - 并更改此分辨率?
- 25. 如何获得Java g.drawString()高分辨率?
- 26. 屏幕分辨率Java代码
- 27. ... Java的屏幕分辨率更改
- 28. Java屏幕分辨率变化
- 29. Java屏幕分辨率大小
- 30. Qt屏幕分辨率初始屏幕
你能提供一些关于你提出的问题的更多细节。一个班轮可以导致数百种不同的方式。 – 2010-09-09 20:17:25
我想你不关心多个显示器的设置。似乎很多应用程序开发人员忽略了这些。在我工作的地方,每个人都使用多台显示器,所以我们总是需要考虑他们。我们探测所有监视器并将它们设置为屏幕对象,以便我们可以在打开新框架时将其定位。如果你真的不需要这个功能,那么我想你可以问这样一个开放式的问题并且很快接受了答案。 – 2010-09-09 20:50:52