主题:居中显示窗口
java代码如下:
int width = Toolkit.getDefaultToolkit().getScreenSize().width;//屏幕宽度
int height = Toolkit.getDefaultToolkit().getScreenSize().height;//屏幕高度
int windowWidth = this.getWidth();//窗口宽度
int windowHeight = this.getHeight();//窗口高度
this.setLocation((width - windowWidth) / 2, (height - windowHeight) / 2);//设置窗口在屏幕正中间显示
最近又学习了一个方法,可以使窗口居中显示,而且更加简单好用:
setLocationRelativeTo(null);
public void setLocationRelativeTo(Component c)
设置窗口相对于指定组件的位置。
如果组件当前未显示,或者 c 为 null,则此窗口将置于屏幕的中央。
中点可以使用 GraphicsEnvironment.getCenterPoint 确定。