private int getScreenWidth(){
int width;
try{
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
width = screenSize.width >= 1024 ? screenSize.width : 1024;
}catch(HeadlessException e){
logger.write("couldnt get screen width" + e);
width = 1024;
}
return width;
}
最佳答案
嗯,不。 HeadlessException得到
thrown when code that is dependent on a keyboard, display, or mouse is called in an environment that does not support a keyboard, display, or mouse.
在没有有屏幕的环境中强制屏幕尺寸是没有意义的。
在这种情况下,应用程序应该退出而不是恢复到默认值。
关于java - 保留默认值并避免异常是否合法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5179699/