如何在java中获得屏幕分辨率?
Posted
技术标签:
【中文标题】如何在java中获得屏幕分辨率?【英文标题】:How can I get screen resolution in java? 【发布时间】:2011-04-10 11:05:35 【问题描述】:如何获得以像素为单位的屏幕分辨率(宽 x 高)?
我正在使用 JFrame 和 java swing 方法。
【问题讨论】:
你能否提供更多关于你所问问题的细节。一个班轮可以导致一百种不同的方式。 我猜你不关心多显示器设置。似乎许多应用程序开发人员忽略了这些。在我工作的地方,每个人都使用多台显示器,所以我们总是要考虑它们。我们探测所有监视器并将它们设置为屏幕对象,以便在打开新帧时可以定位它们。如果你真的不需要这个功能,那么我想你问这样一个开放式的问题并这么快就接受了答案是可以的。 【参考方案1】:您可以使用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 中获得屏幕分辨率,你必须在 Toolkit
上使用 getScreenResolution()
方法。
资源:
javadoc - Toolkit.getScreenSize() Java bug 5100801- Toolkit.getScreenSize() does not return the correct dimension on multimon, linux【讨论】:
这对我不起作用。我有一台 3840x2160 显示器,但getScreenSize
返回 1920x1080。
我使用的是 windows 10,1920 * 1080 但它返回 1280 * 720。还有没有办法使用工具包更改分辨率?【参考方案2】:
此代码将枚举系统上的图形设备(如果安装了多台显示器),您可以使用该信息来确定显示器的亲和性或自动放置(某些系统使用小侧显示器进行实时显示,而应用在后台运行,这样的监视器可以通过大小、屏幕颜色等来识别):
// 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
【讨论】:
【参考方案3】:此电话将为您提供所需的信息。
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
【讨论】:
这只会给出多显示器系统上主显示器的尺寸。见docs.oracle.com/javase/8/docs/api/java/awt/…【参考方案4】:这里有一些函数代码 (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()
【讨论】:
【参考方案5】:这是给定组件当前分配的屏幕分辨率(类似于根窗口的大部分内容在该屏幕上可见)。
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);
【讨论】:
【参考方案6】:这三个函数返回 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;
【讨论】:
【参考方案7】: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);
【讨论】:
【参考方案8】:这是我经常使用的一段代码。它返回完整的可用屏幕区域(即使在多显示器设置上),同时保留本机显示器位置。
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]
【讨论】:
【参考方案9】:int resolution =Toolkit.getDefaultToolkit().getScreenResolution();
System.out.println(resolution);
【讨论】:
【参考方案10】:有很多答案,但我仍然觉得它们还不够,我的方法计算与屏幕尺寸相关的全局变量一次,并且还使用所有监视器的单个循环:
public final class ScreenArea
public static final Rectangle RECTANGLE;
public static final int
LEFT, RIGHT,
TOP, BOTTOM,
MIN_WIDTH, MAX_WIDTH,
MIN_HEIGHT, MAX_HEIGHT,
TOTAL_WIDTH, TOTAL_HEIGHT;
static
// Initialise local vars
int left, right, top, bottom, minWidth, maxWidth, minHeight, maxHeight;
left = top = minWidth = minHeight = Integer.MAX_VALUE;
right = bottom = maxWidth = maxHeight = Integer.MIN_VALUE;
// In a single loop process all bounds
Rectangle bounds;
for (GraphicsDevice device : GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices())
bounds = device.getDefaultConfiguration().getBounds();
if (left > bounds.x)
left = bounds.x;
if (right < bounds.x + bounds.width)
right = bounds.x + bounds.width;
if (top > bounds.y)
top = bounds.y;
if (bottom < bounds.y + bounds.height)
bottom = bounds.y + bounds.height;
if (minWidth > bounds.width)
minWidth = bounds.width;
if (maxWidth < bounds.width)
maxWidth = bounds.width;
if (minHeight > bounds.height)
minHeight = bounds.height;
if (maxHeight < bounds.height)
maxHeight = bounds.height;
TOTAL_WIDTH = right - left;
TOTAL_HEIGHT = bottom - top;
RECTANGLE = new Rectangle(TOTAL_WIDTH, TOTAL_HEIGHT);
// Transfer local to immutable global vars
LEFT = left; RIGHT = right; TOP = top; BOTTOM = bottom;
MIN_WIDTH = minWidth; MAX_WIDTH = maxWidth;
MIN_HEIGHT = minHeight; MAX_HEIGHT = maxHeight;
然后你可以像这样随时使用:
System.out.printf("LEFT=%d, ", ScreenArea.LEFT);
System.out.printf("RIGHT=%d%n", ScreenArea.RIGHT);
System.out.printf("TOP=%d, ", ScreenArea.TOP);
System.out.printf("BOTTOM=%d%n", ScreenArea.BOTTOM);
System.out.printf("MIN_WIDTH=%d, ", ScreenArea.MIN_WIDTH);
System.out.printf("MAX_WIDTH=%d%n", ScreenArea.MAX_WIDTH);
System.out.printf("MIN_HEIGHT=%d, ", ScreenArea.MIN_HEIGHT);
System.out.printf("MAX_HEIGHT=%d%n", ScreenArea.MAX_HEIGHT);
System.out.printf("SCREEN_AREA=%s%n", ScreenArea.RECTANGLE);
它在我的双显示器设置上打印:
LEFT=0, RIGHT=3840
TOP=0, BOTTOM=1080
MIN_WIDTH=1920, MAX_WIDTH=1920
MIN_HEIGHT=1080, MAX_HEIGHT=1080
SCREEN_AREA=java.awt.Rectangle[x=0,y=0,width=3840,height=1080]
【讨论】:
我工作得很好,谢谢,并且允许在放置窗口时编写可读的逻辑。【参考方案11】:int screenResolution = Toolkit.getDefaultToolkit().getScreenResolution();
System.out.println(""+screenResolution);
【讨论】:
欢迎来到 Stack Overflow!虽然这段代码 sn-p 可以解决问题,但including an explanation 确实有助于提高帖子的质量。请记住,您正在为将来的读者回答问题,而这些人可能不知道您的代码建议的原因。也请尽量不要用解释性的 cmets 挤满你的代码,这会降低代码和解释的可读性!【参考方案12】:不幸的是,如果您有多个显示器,Toolkit.getDefaultToolkit()
将无济于事,并且在 Windows 上,如果您将字体设置“缩放和布局”从 100% 更改,它也会报告缩放值。例如,在 150% 的字体比例下,我的 1920x1080 屏幕报告为 1280x720,这(无益地)改变了我的应用使用的分辨率。
相反,我使用这种方法读取每个 GraphicsDevice
的默认显示模式来访问原始屏幕位置+尺寸,并返回按屏幕左->右 X 位置顺序排序的矩形集:
/** Get actual screen display sizes, ignores Windows font scaling, sort left to right */
public static List<Rectangle> getDisplays()
return Arrays.stream(GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices())
.map(GraphicsDevice::getDefaultConfiguration)
// For scaled sizes use .map(GraphicsConfiguration::getBounds) instead of:
.map(c ->
var dm = c.getDevice().getDisplayMode();
var bounds = c.getBounds();
return new Rectangle((int)bounds.getX(), (int)bounds.getY(), dm.getWidth(), dm.getHeight());
)
.sorted(Comparator.comparing(Rectangle::getX))
.toList();
以上代码在Windows和WSL下运行。
【讨论】:
以上是关于如何在java中获得屏幕分辨率?的主要内容,如果未能解决你的问题,请参考以下文章