第1种方法:
//setSize(300, 200);
pack();
// 得到显示器屏幕的宽、高
int width = Toolkit.getDefaultToolkit().getScreenSize().width;
int height = Toolkit.getDefaultToolkit().getScreenSize().height;
// 得到窗体的宽、高
int windowsWidth = this.getWidth();
int windowsHeight = this.getHeight();
//System.out.println(windowsWidth+","+windowsHeight);
this.setBounds((width - windowsWidth) / 2,(height - windowsHeight) / 2, windowsWidth, windowsHeight);
第2种方法:
setLocationRelativeTo(null);