How to set the center position of the Main Window in Java Swing
01. method 1
Int required wwidth = frame. getWidth (); // obtain the window width.
Int required wheight = frame. getHeight (); // get the window height
Toolkit kit = Toolkit. getdefatooltoolkit (); // define the Toolkit
Dimension screenSize = kit. getScreenSize (); // obtain the screen size
Int screenWidth = screenSize. width; // obtain the screen width.
Int screenHeight = screenSize. height; // obtain the screen height.
Frame. setLocation (screenWidth/2-0000wwidth/2, screenHeight/2-0000wheight/2); // you can specify that the window is centered.
02. method 2
Toolkit kit = Toolkit. getdefatooltoolkit (); // define the Toolkit
Dimension screenSize = kit. getScreenSize (); // obtain the screen size
Int screenWidth = screenSize. width/2; // obtain the screen width.
Int screenHeight = screenSize. height/2; // obtain the screen height.
Int height = this. getHeight (); int width = this. getWidth (); setLocation (screenWidth-width/2, screenHeight-height/2 );
03. The third method is provided after jdk1.4.
SetLocationRelativeTo (owner );
This method sets the position of a window relative to another window (usually centered in the middle of the parent window). If owner = null, the window is located in the center of the screen.