Just click on form and go to JFrame properties, then Code tab and check Generate Center .
To change the position of JFrame on the screen, JFrame provides the method JFrame. setlocation(int x, int y), you need two parameters 'x' represents the position of the x axis and 'y' represents the position of the y axis. The upper left corner of your screen is (0,0). If you give NULL as parameter to JFrame.
Calling setDefaultCloseOperation(EXIT_ON_CLOSE) does exactly this. It causes the application to exit when the application receives a close window event from the operating system.
Use setLocationRelativeTo(null)
This method has a special effect when you pass it a null
. According to the Javadoc:
If the component is null, or the GraphicsConfiguration associated with this component is null, the window is placed in the center of the screen.
This should be done after setting the size or calling pack()
, but before setting it visible, like this:
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
I always did it in this way:
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation(dim.width/2-this.getSize().width/2, dim.height/2-this.getSize().height/2);
where this
is the JFrame involved.
You can call JFrame.setLocationRelativeTo(null)
to center the window. Make sure to put this before JFrame.setVisible(true)
Just click on form and go to JFrame properties, then Code tab and check Generate Center
.
As simple as this...
setSize(220, 400);
setLocationRelativeTo(null);
or if you are using a frame then set the frame to
frame.setSize(220, 400);
frame.setLocationRelativeTo(null);
For clarification, from the docs:
If the component is null, or the GraphicsConfiguration associated with this component is null, the window is placed in the center of the screen.
i am using NetBeans IDE 7.2.1 as my developer environmental and there you have an option to configure the JForm properties.
in the JForm Properties go to the 'Code' tab and configure the 'Generate Center'. you will need first to set the Form Size Policy to 'Generate Resize Code'.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With