我正在尝试让JavaFX检测我正在使用的屏幕分辨率,无论是1080p还是4k,因此我可以相应地调整/定位我的程序。
我试过Screen.getPrimary().getDpi()
并打印出“96”。使用Toolkit.getDefaultToolkit().getScreenResolution()
我得到了“144”的结果。我不知道哪一个是正确的,或者是否有更好的方法。
请帮忙。谢谢。
您实际上想知道屏幕尺寸而不是像素密度。 DPI是Dots Per Inch的衡量标准。
在JavaFX中,您可以使用Screen class实现此目的。由于名为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
在代码段中使用主屏幕。您还可以使用Screen.getScreens()
获取其他屏幕。
我刚刚找到了另一种使用AWT GraphicsEnvironment查找屏幕宽度的方法。代码如下:
GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDisplayMode().getWidth()
这种方法给我“真正的”4k屏幕宽度:3840
在Javafx中使用以下方法,宽度为2560。
Rectangle2D visualBounds = Screen.getPrimary().getVisualBounds();
对于遇到类似问题的人来说,思考可能是一个有用的信息。