Java Swing Set main form position centering method

Source: Internet
Author: User

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

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.