You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取Java Swing应用所在显示器的屏幕分辨率?

问题:副屏启动Swing应用时无法获取对应屏幕分辨率

我编写了一个Java类用于获取屏幕分辨率,以此设置Swing组件尺寸。在主屏运行时一切正常,但在副屏启动应用时,这个类只能获取主屏的分辨率。需要找到方法检测应用所在的显示器并获取其屏幕尺寸。

原代码

package com.mcsolution.common.Componenti_Swing;

import java.awt.Dimension;
import java.awt.Toolkit;

public class Dimensione extends Dimension {
    Dimension screenSize;
    double larghezza, altezza;

    public Dimensione() {
        Toolkit t = Toolkit.getDefaultToolkit();
        screenSize = t.getScreenSize();
        double width = screenSize.getWidth();
        double height = screenSize.getHeight() * 0.9;
        this.setSize(width, height);
    }

    /********PANEL******************/
    public Dimension DimensionePanel() {
        larghezza = screenSize.getWidth();
        altezza = screenSize.getHeight() * 0.95;
        this.setSize(larghezza, altezza);
        return this;
    }

    public Dimension PanelArticoliScontrini() {
        larghezza = screenSize.getWidth();
        altezza = screenSize.getHeight() * 0.65;
        this.setSize(larghezza, altezza);
        return this;
    }

    public Dimension PanelArticoliDaStampare() {
        larghezza = screenSize.getWidth();
        altezza = screenSize.getHeight() * 0.90;
        this.setSize(larghezza, altezza);
        return this;
    }
}

解决方案

Toolkit.getDefaultToolkit().getScreenSize()只能获取系统默认主屏的尺寸,要获取应用所在显示器的尺寸,需要借助GraphicsEnvironment和GraphicsDevice,或者直接基于窗口组件获取其所在屏幕的配置。

方法1:基于窗口组件获取所在屏幕尺寸

如果应用已有顶层窗口(比如JFrame),直接通过窗口的GraphicsConfiguration获取其所在屏幕的边界:

package com.mcsolution.common.Componenti_Swing;

import java.awt.Dimension;
import java.awt.GraphicsConfiguration;
import java.awt.Rectangle;
import java.awt.Toolkit;
import javax.swing.JFrame;

public class Dimensione extends Dimension {
    Dimension screenSize;

    // 传入顶层窗口,获取其所在屏幕的尺寸
    public Dimensione(JFrame frame) {
        GraphicsConfiguration config = frame.getGraphicsConfiguration();
        Rectangle screenBounds = config.getBounds();
        
        // 可选择排除任务栏等系统Insets
        int insetsTop = Toolkit.getDefaultToolkit().getScreenInsets(config).top;
        int insetsBottom = Toolkit.getDefaultToolkit().getScreenInsets(config).bottom;
        
        double width = screenBounds.getWidth();
        double height = (screenBounds.getHeight() - insetsTop - insetsBottom) * 0.9;
        this.setSize(width, height);
        screenSize = new Dimension((int)screenBounds.getWidth(), (int)screenBounds.getHeight());
    }

    // 原有面板尺寸计算方法,改用当前屏幕尺寸
    public Dimension DimensionePanel() {
        double larghezza = screenSize.getWidth();
        double altezza = screenSize.getHeight() * 0.95;
        this.setSize(larghezza, altezza);
        return this;
    }

    public Dimension PanelArticoliScontrini() {
        double larghezza = screenSize.getWidth();
        double altezza = screenSize.getHeight() * 0.65;
        this.setSize(larghezza, altezza);
        return this;
    }

    public Dimension PanelArticoliDaStampare() {
        double larghezza = screenSize.getWidth();
        double altezza = screenSize.getHeight() * 0.90;
        this.setSize(larghezza, altezza);
        return this;
    }
}

方法2:通过鼠标位置检测启动所在屏幕(适合窗口创建前初始化)

如果需要在窗口创建前获取启动位置的屏幕,可通过鼠标位置判断:

package com.mcsolution.common.Componenti_Swing;

import java.awt.Dimension;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;

public class Dimensione extends Dimension {
    Dimension screenSize;

    public Dimensione() {
        // 获取所有屏幕设备
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice[] screens = ge.getScreenDevices();
        
        // 获取当前鼠标位置
        Point mousePos = java.awt.MouseInfo.getPointerInfo().getLocation();
        
        Rectangle targetScreenBounds = null;
        for (GraphicsDevice screen : screens) {
            Rectangle bounds = screen.getDefaultConfiguration().getBounds();
            if (bounds.contains(mousePos)) {
                targetScreenBounds = bounds;
                break;
            }
        }
        
        // 鼠标不在任何屏幕范围内时, fallback到默认屏幕
        if (targetScreenBounds == null) {
            targetScreenBounds = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
        }
        
        double width = targetScreenBounds.getWidth();
        double height = targetScreenBounds.getHeight() * 0.9;
        this.setSize(width, height);
        screenSize = new Dimension((int)targetScreenBounds.getWidth(), (int)targetScreenBounds.getHeight());
    }

    // 原有面板尺寸计算方法保持不变
    public Dimension DimensionePanel() {
        double larghezza = screenSize.getWidth();
        double altezza = screenSize.getHeight() * 0.95;
        this.setSize(larghezza, altezza);
        return this;
    }

    public Dimension PanelArticoliScontrini() {
        double larghezza = screenSize.getWidth();
        double altezza = screenSize.getHeight() * 0.65;
        this.setSize(larghezza, altezza);
        return this;
    }

    public Dimension PanelArticoliDaStampare() {
        double larghezza = screenSize.getWidth();
        double altezza = screenSize.getHeight() * 0.90;
        this.setSize(larghezza, altezza);
        return this;
    }
}

说明

  • 方法1更准确,直接绑定应用窗口所在屏幕,适合窗口已创建的场景。
  • 方法2适合窗口初始化前的场景,通过鼠标位置判断启动时所在的显示器。

内容的提问来源于stack exchange,提问作者bircastri

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 01:50:59