如何让JFrame显示在屏幕中间?

如何让JFrame显示在屏幕中间?

1.double width = Toolkit.getDefaultToolkit().getScreenSize().getWidth();
double height = Toolkit.getDefaultToolkit().getScreenSize().getHeight();
setLocation( (int) (width - this.getWidth()) / 2,(int) (height - this.getHeight()) / 2);
show();

2.frame.setLocation( (screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);