【发布时间】:2011-07-01 06:42:01
【问题描述】:
我正在开发一个需要屏幕 DPI 的应用程序。我检查了几个论坛并得到了一个代码 sn-p,如下所示:
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);
但是无论我的屏幕分辨率是多少,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();
}
但同样无论我的屏幕分辨率是多少,getDPI() 返回的值都是相同的 96.. 出了什么问题?我的代码是错误的还是我以错误的方式解释它??
【问题讨论】: