JFrame centered on the screen

P

palmis

How can I create a JFrame that is centered on the
screen?
It appear up on the left corner of screen. Why?

Palmis
 
T

Thomas Fritsch

palmis said:
How can I create a JFrame that is centered on the
screen?
JFrame frame = ...;
frame.setLocationRelativeTo(null);

See the API doc of java.awt.Window#setLocationRelativeTo.
JFrame inherits that method from java.awt.Window.
It appear up on the left corner of screen. Why?
Because the default position of a JFrame is (0,0).
 
K

Knute Johnson

Thomas said:
JFrame frame = ...;
frame.setLocationRelativeTo(null);

See the API doc of java.awt.Window#setLocationRelativeTo.
JFrame inherits that method from java.awt.Window.


Because the default position of a JFrame is (0,0).

And remember that you have to pack or size it before you call
setLocationRelativeTo().
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top