笔记记录

编写人:老王
时间:2023-04-4
地点:广州


screen获取屏幕信息

  1. package javafx.test;
  2. import javafx.application.Application;
  3. import javafx.application.Platform;
  4. import javafx.geometry.Rectangle2D;
  5. import javafx.stage.Screen;
  6. import javafx.stage.Stage;
  7. public class Main4 extends Application {
  8. public static void main(String[] args) {
  9. launch(args);
  10. }
  11. @Override
  12. public void start(Stage stage) throws Exception {
  13. Screen screen = Screen.getPrimary();
  14. //获取全部的屏幕的宽度和高度,看不到的也能获取
  15. Rectangle2D rec1 = screen.getBounds();
  16. //获取用户可视的屏幕的宽度和高度
  17. Rectangle2D rec2 = screen.getVisualBounds();
  18. //下面是全部屏幕宽高和坐标
  19. System.out.println("左上角x = "+ rec1.getMinX() + " " +
  20. "左下角y = " + rec1.getMinY());
  21. System.out.println("右下角x = "+ rec1.getMaxX() + " " +
  22. "右下角y = " + rec1.getMaxY());
  23. Platform.exit();
  24. }
  25. }

图片.png获取屏幕的宽和高:

  1. System.out.println("宽度 = "+ rec1.getWidth() + " " +
  2. "高度 = " + rec1.getHeight());

图片.png最终的完整查看可视屏幕和完整屏幕的代码如下:

  1. package javafx.test;
  2. import javafx.application.Application;
  3. import javafx.application.Platform;
  4. import javafx.geometry.Rectangle2D;
  5. import javafx.stage.Screen;
  6. import javafx.stage.Stage;
  7. public class Main4 extends Application {
  8. public static void main(String[] args) {
  9. launch(args);
  10. }
  11. @Override
  12. public void start(Stage stage) throws Exception {
  13. Screen screen = Screen.getPrimary();
  14. //获取全部的屏幕的宽度和高度,看不到的也能获取
  15. Rectangle2D rec1 = screen.getBounds();
  16. //获取用户可视的屏幕的宽度和高度
  17. Rectangle2D rec2 = screen.getVisualBounds();
  18. //下面是全部屏幕宽高和坐标
  19. System.out.println("下面是全部屏幕的宽高和坐标");
  20. System.out.println("左上角x = "+ rec1.getMinX() + " " +
  21. "左下角y = " + rec1.getMinY());
  22. System.out.println("右下角x = "+ rec1.getMaxX() + " " +
  23. "右下角y = " + rec1.getMaxY());
  24. System.out.println("宽度 = "+ rec1.getWidth() + " " +
  25. "高度 = " + rec1.getHeight());
  26. System.out.println("下面是可以看到的屏幕的宽高和坐标");
  27. System.out.println("左上角x = "+ rec2.getMinX() + " " +
  28. "左下角y = " + rec2.getMinY());
  29. System.out.println("右下角x = "+ rec2.getMaxX() + " " +
  30. "右下角y = " + rec2.getMaxY());
  31. System.out.println("宽度 = "+ rec2.getWidth() + " " +
  32. "高度 = " + rec2.getHeight());
  33. Platform.exit();
  34. }
  35. }

图片.png
可以看到完整的和可视的 宽度和高度是不一样。

查看当前屏幕的分辨率DPI

  1. package javafx.test;
  2. import javafx.application.Application;
  3. import javafx.application.Platform;
  4. import javafx.geometry.Rectangle2D;
  5. import javafx.stage.Screen;
  6. import javafx.stage.Stage;
  7. public class Main4 extends Application {
  8. public static void main(String[] args) {
  9. launch(args);
  10. }
  11. @Override
  12. public void start(Stage stage) throws Exception {
  13. Screen screen = Screen.getPrimary();
  14. double dpi = screen.getDpi();
  15. System.out.println("当前屏幕分辨率DPI为:" + dpi);
  16. Platform.exit();
  17. }
  18. }

图片.png