|
-
Feb 4th, 2008, 01:22 PM
#1
Thread Starter
Hyperactive Member
Center jFrame
How can I center a jFrame on screen when it loads up? Im using netbeans 6.0.
-
Feb 4th, 2008, 02:46 PM
#2
Fanatic Member
Re: Center jFrame
i dont think there is a way
I Use SetLocation method using 2 parameter width & height
-
Feb 4th, 2008, 03:06 PM
#3
Thread Starter
Hyperactive Member
Re: Center jFrame
I tried this but it didnt work:
Code:
this.setLocation(this.WIDTH,this.HEIGHT);
Where should I put the code and what do I need to change in order for it to work?
Thanks for your help
-
Feb 4th, 2008, 03:21 PM
#4
Fanatic Member
Re: Center jFrame
Code:
this.setLocation(300,200);
-
Feb 4th, 2008, 06:42 PM
#5
Thread Starter
Hyperactive Member
Re: Center jFrame
This works pretty sweet:
Code:
public static void centerFrame(JFrame frame) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
Point center = ge.getCenterPoint();
Rectangle bounds = ge.getMaximumWindowBounds();
int w = Math.max(bounds.width/2, Math.min(frame.getWidth(), bounds.width));
int h = Math.max(bounds.height/2, Math.min(frame.getHeight(), bounds.height));
int x = center.x - w/2, y = center.y - h/2;
frame.setBounds(x, y, w, h);
if (w == bounds.width && h == bounds.height)
frame.setExtendedState(Frame.MAXIMIZED_BOTH);
frame.validate();
}
-
Feb 4th, 2008, 06:51 PM
#6
Fanatic Member
Re: Center jFrame
Yes maybe i always use setLocation Not only for the Frame but all other elements because i use JCreator I can only Code !
-
Feb 5th, 2008, 04:25 AM
#7
Re: Center jFrame
Try this code, better performance:
Code:
Dimension screenResolution = this.getToolkit().getScreenSize();
this.setLocation((screenResolution.width - this.getWidth()) / 2, (screenResolution.height - this.getHeight()) / 2);
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|