And*_*rao 128 java swing resolution screen
如何以像素为单位获得屏幕分辨率(宽x高)?
我正在使用JFrame和java swing方法.
Col*_*ert 251
您可以使用该Toolkit.getScreenSize()
方法获取屏幕大小.
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
double width = screenSize.getWidth();
double height = screenSize.getHeight();
Run Code Online (Sandbox Code Playgroud)
在多显示器配置上,您应该使用:
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
int width = gd.getDisplayMode().getWidth();
int height = gd.getDisplayMode().getHeight();
Run Code Online (Sandbox Code Playgroud)
如果要在DPI中获得屏幕分辨率,则必须使用该getScreenResolution()
方法Toolkit
.
资源:
Ric*_*gin 15
此代码将枚举系统上的图形设备(如果安装了多个监视器),并且您可以使用该信息来确定监视器关联或自动放置(某些系统在应用程序运行时使用一个小侧监视器进行实时显示背景,这样的监视器可以通过大小,屏幕颜色等来识别.):
// 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
}
Run Code Online (Sandbox Code Playgroud)
Sta*_*key 11
此调用将为您提供所需的信息.
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Run Code Online (Sandbox Code Playgroud)
不幸的是,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());
Run Code Online (Sandbox Code Playgroud)
我使用此方法读取每个屏幕的默认显示模式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();
}
Run Code Online (Sandbox Code Playgroud)
上面的代码在Windows和WSL下运行。如果您希望有一个返回缩放值的版本,只需切换上面的注释行即可。
归档时间: |
|
查看次数: |
214769 次 |
最近记录: |