How to set the center position of the Main Window in Java Swing

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.