如何在Java中获取屏幕分辨率?

163

如何获取屏幕分辨率(宽x高)的像素值?

我正在使用JFrame和Java Swing方法。


2
你能提供更多关于你问题的细节吗?一个简短的描述可能会引出无数不同的方式。 - Anil Vishnoi
8
我猜你不关心多显示器设置。似乎很多应用程序开发者忽略了这一点。在我工作的地方,每个人都使用多个显示器,所以我们必须考虑它们。我们探测所有的显示器并将它们设置为屏幕对象,这样我们就可以在打开新窗口时针对它们进行定位。如果你真的不需要这种功能,那么我想你问一个如此开放式的问题并很快接受一个答案也是可以的。 - Erick Robertson
12个回答

311

你可以使用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() 方法。 资源:

10
这对我没用。我有一台3840x2160的显示器,但是getScreenSize返回的是1920x1080。 - ZhekaKozlov
我正在使用 Windows 10,分辨率为 1920 * 1080,但它返回 1280 * 720。另外,是否有一种方法可以使用工具包更改分辨率? - prachi
@prachi 嗯,我认为这是因为你使用的分辨率比你的屏幕实际分辨率低(或者在Windows上使用更高的缩放比例)。你可以尝试我提供的解决方案,它可以计算任意数量屏幕的总屏幕面积,而不使用不一致的 Toolkit#getScreenSize() - Lorenzo

17

此代码将枚举系统上的图形设备(如果安装了多个监视器),您可以使用该信息确定监视器关联性或自动放置(某些系统在后台运行应用程序时使用小侧面监视器进行实时显示,这样的监视器可以通过大小、屏幕颜色等来识别):

// 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
}

12

这个调用会为您提供所需的信息。

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

1
这将只提供多监视器系统上主显示器的尺寸。请参见https://docs.oracle.com/javase/8/docs/api/java/awt/Toolkit.html#getScreenSize-- - Nathan

5

很遗憾,如果您有多个显示器,Toolkit.getDefaultToolkit()无法帮助您,在Windows上,如果您将字体设置中的“缩放和布局”更改为非100%,则还会报告缩放值。例如,当字体比例为150%时,我的1920x1080屏幕被报告为1280x720,这(不方便地)更改了应用程序使用的分辨率。

您可以访问所有屏幕设备的详细信息:

GraphicsDevice[] devices = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();

// Windows scaled sizes (eg 1280x720 for my case at 150% scaling)
Rectangle bounds = devices[nnn].getDefaultConfiguration().getBounds();

// Display sizes (same as above at 100% scale, 1920x1080 for my case)
DisplayMode dm = devices[nnn].getDefaultConfiguration().getDevice().getDisplayMode();
Rectangle orig = new Rectangle((int)bounds.getX(), (int)bounds.getY(), dm.getWidth(), dm.getHeight());

我使用以下方法来读取每个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下运行。如果您希望有一个返回缩放值的版本,只需切换上面被注释的行。


这些信息真的救了我的一天!在Windows中,扩展现在是一个非常流行的选项,所以我不明白为什么其他解决方案没有提到它! - paulo116

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()

这些链接提供了有关Java中的图形环境和设备配置的信息。

4

这三个函数可以返回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;
}

3

该组件当前分配的屏幕分辨率(类似于根窗口的大部分可见在该屏幕上)。

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);

2

虽然有很多答案,但我仍然觉得它们不够充分,我的方法计算与屏幕大小相关的全局变量一次,并且使用单个循环遍历所有监视器:

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;
    }
}

然后你可以像这样随时使用 anytime:
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);

在我双显示器设置中,它打印的内容是:

(保留HTML标签)

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]

1
它运行得非常完美,谢谢,并且允许在放置窗口时编写可读的逻辑。 - Eric Duminil

2

这是我经常使用的一段代码。它返回完整的可用屏幕区域(即使在多监视器设置中),同时保留本机监视器位置。

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]

2
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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接