Java graphical interface programming---------the form is loaded in the middle

Source: Internet
Author: User

Java graphical interface programming---------the form is loaded in the middle


Many times when we create a Java interface application, we all want to be able to initialize the form to the screen when the program is run


In the middle, that's how good! Next i'll show you two ways to center the Form.




one, Method One


Use the Setlocationrelativeto (Component C) in Java.awt.Window to set the Method.


Setlocationrelativeto (Component c);

Sets the location of the windows relative to the specified component according to the following Scenarios.

The target screen mentioned below are a screen to which the window should being placed after the Setlocationrelativeto method Is Called.

If the component is null (if empty), or the graphicsconfiguration associated with this component are null, the window is placed In the center of the Screen. The center point can is obtained with the Graphicsenvironment.getcenterpoint method.

If the component is isn't null, but it isn't currently showing, the window is placed in the center of the target screen def Ined by Thegraphicsconfiguration Associated and this Component.

If the component is not null and was shown on the screens, then the window was located in such a, the center of the W Indow coincides with the center of the Component.


The code is as Follows:

Import Java.awt.event.windowadapter;import Java.awt.event.windowevent;import Javax.swing.jframe;public class Loadposition extends jframe{private static final long serialversionuid = 1l;public loadposition () {this.settitle ("Load_ Position "); this.setsize (700,500); this.setvisible (true); this.setdefaultcloseoperation (jframe.exit_on_close);// Set the event listener for the current window, using the window adapter This.addwindowlistener (new windowadapter () {@Overridepublic void windowopened (windowevent E {//this method is called Setlocationrelativeto (null);}});} public static void main (string[] Args) {new loadposition ();}}



second, Method Two


Public Frame01 () {super ("Frame01"); this.setresizable (false);//set the size of the form this.setsize (800, 500);// Sets the background color This.getcontentpane (). setbackground (new Color (152,251,152)),//dimension encapsulates the width and height of the computer screen//gets the screen width and height, Make the window in the middle of the screen dimension Width=toolkit.getdefaulttoolkit (). getscreensize (); this.setlocation ((int) (width.getwidth ()- 799)/2, (int) (width.getheight ()-500)/2);

This article is from the "11941149" blog, please be sure to keep this source http://11951149.blog.51cto.com/11941149/1851444

Java graphical interface programming---------the form is loaded in the middle

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.