01. The first method
int windowwidth = Frame.getwidth (); Get window width
int windowheight = Frame.getheight (); Get window Height
Toolkit kit = Toolkit.getdefaulttoolkit (); Defining the Toolkit
Dimension screensize = Kit.getscreensize (); Get the size of the screen
int screenwidth = Screensize.width; Gets the width of the screen
int screenheight = Screensize.height; Gets the height of the screen
Frame.setlocation (SCREENWIDTH/2-WINDOWWIDTH/2, SCREENHEIGHT/2-WINDOWHEIGHT/2);//Set Window center display
02. The second method
Toolkit kit = Toolkit.getdefaulttoolkit (); Defining the Toolkit
Dimension screensize = Kit.getscreensize (); Get the size of the screen
int screenwidth = SCREENSIZE.WIDTH/2; Gets the width of the screen
int screenheight = SCREENSIZE.HEIGHT/2; Gets the height of the screen
int height = this.getheight (); int width = this.getwidth (); SetLocation (SCREENWIDTH-WIDTH/2, SCREENHEIGHT-HEIGHT/2);
03, the third method, is the method provided after jdk1.4
Setlocationrelativeto (owner);
This approach is to set the position of a window relative to another window (typically centered in the middle of the parent window), and if owner==null the window resides in the center of the screen.
Java Swing Set main window position centering method