01. The first method
int windowwidth = Frame.getwidth (); Get form width
int windowheight = Frame.getheight (); Get form High
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 the form to center the display
02. Another 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);
The way to do this is to set the position of one form relative to the other (usually centered in the middle of the parent form). Suppose owner==null the form is centered on the screen.
Java Swing Set main form position centering method