在所创建的窗口类的构造方法中加入下列代码:
Toolkit kit = Toolkit.getDefaultToolkit(); //定义工具包
Dimension screenSize = kit.getScreenSize(); //获取屏幕的尺寸
int screenWidth = screenSize.width; //获取屏幕的宽
int screenHeight = screenSize.height; //获取屏幕的高
int windowWidth = this.getWidth(); //获得窗口宽
int windowHeight = this.getHeight();//获得窗口高
//设置窗口居中显示
this.setLocation(this.screenWidth/2-windowWidth/2, this.screenHeight/2-windowHeight/2);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置默认关闭模式