获取当前屏幕分辨率及当前窗口大小
//获取屏幕分辨率及当前窗口大小,位置居中显示
int x = this.getWidth();
int y = this.getHeight();
int screenWidth=((int)java.awt.Toolkit.getDefaultToolkit().getScreenSize().width);
int screenHeight = ((int)java.awt.Toolkit.getDefaultToolkit().getScreenSize().height);
this.setLocation((screenWidth-x)/2, (screenHeight-y)/2-30);
//实例:
private void setWindowToScreenCenter(Window w) {
Dimension size = w.getSize();
Dimension scsize = Toolkit.getDefaultToolkit().getScreenSize();
int x = (scsize.width - size.width) / 2;
int y = (scsize.height - size.height) / 2;
if (x < 0)
x = 0;
if (y < 0)
y = 0;
w.setLocation(x, y);
}
网友评论