While working with Java, I find it hard to position my main window in the center of the screen when I start the application.
Is there any way I can do that? It doesn't have to be vertically centered, horizontal alignment is the more important goal for me. But vertical alignment is also welcome.
I am using NetBeans IDE 7.3 and this is how I go about centralizing my JFrame Make sure you click on the JFrame Panel and go to your JFrame property bar,click on the Code bar and select Generate Center check box.
You can use this method, which allows the JFrame to be centered and full screen at the same time.
If you use NetBeans, simply click on the frame on the design view, then the code tab on its properties. Next, check 'Generate Center'. That will get the job done.
I always did it in this way:
where
this
is the JFrame involved.Just click on form and go to JFrame properties, then Code tab and check
Generate Center
.Use
setLocationRelativeTo(null)
This method has a special effect when you pass it a
null
. According to the Javadoc:This should be done after setting the size or calling
pack()
, but before setting it visible, like this: