如何让 JavaFX 判断我是在 4k 还是 1080p 屏幕上

Geo*_*ang 3 javafx screen screen-resolution

我试图让 JavaFX 检测我所使用的屏幕分辨率(1080p 或 4k),以便我可以相应地调整程序的大小/位置。

我尝试了Screen.getPrimary().getDpi()一下,它打印出“96”。使用Toolkit.getDefaultToolkit().getScreenResolution()我得到的结果是“144”。我不知道哪一个是正确的,也不知道是否有更好的方法。

请帮忙。谢谢。

cre*_*not 6

您实际上想知道屏幕尺寸而不是像素密度DPI是每英寸点数的测量单位。

在 JavaFX 中,您可以通过Screen 类来实现这一点。正如名为Jewelsea的用户指出的那样,视觉边界仅返回所选屏幕的视觉区域的值。这意味着您要用于screenObject.getVisualBounds()实际screenObject.getBounds()屏幕尺寸。

 // full bounds
 Rectangle2D bounds = Screen.getPrimary().getBounds();

 bounds.getWidth(); // screens width
 bounds.getHeight(); // screens height

 // visual bounds
 Rectangle2D visualBounds = Screen.getPrimary().getVisualBounds();

 visualBounds.getWidth(); // screens usable width (no task bars etc.)
 visualBounds.getHeight(); // screens usable height
Run Code Online (Sandbox Code Playgroud)

在代码片段中使用主屏幕。您还可以使用 来获取其他屏幕Screen.getScreens()