如何在java中获取屏幕DPI?

cod*_*der 6 java screen-resolution

我正在开发一个应用程序,我需要屏幕DPI ..我检查了几个论坛,并获得了一个代码片段,如下所示:

Dimension screen = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
System.out.println("screen width: "+screen.getWidth()); 
System.out.println("screen height: "+screen.getHeight()); 
int pixelPerInch=java.awt.Toolkit.getDefaultToolkit().getScreenResolution(); 
System.out.println("pixelPerInch: "+pixelPerInch); 

double height=screen.getHeight()/pixelPerInch; 
double width=screen.getWidth()/pixelPerInch; 
double x=Math.pow(height,2); 
double y=Math.pow(width,2); 
Run Code Online (Sandbox Code Playgroud)

但无论屏幕分辨率的pixelPerInch值是多少,96 的值都保持不变.代码有什么问题?

我得到了另一个swt相同的代码,如下所示:

  import org.eclipse.swt.graphics.Device;
  import org.eclipse.swt.widgets.Display;
  import org.eclipse.swt.widgets.Shell;

  public class MainClass {
  public void run() {
  Display display = new Display();
  Shell shell = new Shell(display);
  shell.setText("Display Device");
  createContents(shell);
  shell.pack();
  shell.open();
  while (!shell.isDisposed()) {
    if (!display.readAndDispatch()) {
      display.sleep();
    }
  }
  display.dispose();
}

private void createContents(Shell shell) {
  Device device = shell.getDisplay();

  System.out.println("getBounds(): "+ device.getBounds());
  System.out.println("getClientArea(): "+device.getClientArea());
  System.out.println("getDepth(): "+device.getDepth());
  System.out.println("getDPI(): "+device.getDPI());

  device.setWarnings(true);
  System.out.println("Warnings supported: "+device.getWarnings());
}

public static void main(String[] args) {
  new MainClass().run();
}
Run Code Online (Sandbox Code Playgroud)

但是这里也是我的屏幕分辨率,getDPI()返回相同的值96 ..出了什么问题?我的代码是错的还是我以错误的方式解释它?

Bar*_*own 6

问题是没有人,甚至操作系统都不知道屏幕的确切物理尺寸.你需要知道为了计算PPI.

控制面板中有一个显示设置,用户可以手动指定PPI,默认情况下设置为96.