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

How to make JavaFX tell if I'm on a 4k or 1080p screen

我正在尝试让 JavaFX 检测我使用的屏幕分辨率,1080p 或 4k,因此我可以 size/position 我的程序相应地。

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

请帮忙。谢谢

您实际上想知道屏幕 大小 而不是像素 密度DPIDots Per Inch 的测量值。

在 JavaFX 中,您可以通过 Screen class. As the user named jewelsea 指出 视觉边界 仅 return [=23] 的值来实现此目的=]visual 所选屏幕的区域。这意味着您要为此使用 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

在代码片段中,使用了屏幕。您还可以使用 Screen.getScreens().

获取其他屏幕

我刚刚找到了另一种使用 AWT GraphicsEnvironment 查找屏幕宽度的方法。代码如下:

     GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDisplayMode().getWidth()

这个方法给我 "true" 4k 屏幕宽度:3840

在Javafx中使用如下方法,宽度为2560。

Rectangle2D visualBounds = Screen.getPrimary().getVisualBounds();

对于遇到类似问题的人来说,想法可能是有用的信息。