Discussion Locked

JFrame

0 helpful
6 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

i have JFrame of size(400,300) i want to diaplay this frame at the center fo screen, can you please help me out.??

thanks
Hiral

6 replies

by okujava

here it is:
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int x = (screenSize.width - frame.getWidth())/2;
int y = (screenSize.height - frame.getHeight())/2;
frame.setLocation(x, y);

it is positioned in the center no matter what's the size of the frame.

by diva_cen2000

regards,
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension dframe=getSize();
int x = (screenSize.width - dframe.getWidth())/2;
int y = (screenSize.height - dframe.getHeight())/2;
frame.setLocation(x, y);

by diva_cen2000

sorry ,
add this line after first two lines

if(dframe.width>screenSize.width)
dframe.width=screenSize.width;
if(dframe.heigth>screenSize.heigth)
dframe.height=screenSize.height;

This discussion is closed to new replies.