How can I center a jFrame on screen when it loads up? Im using netbeans 6.0.
Printable View
How can I center a jFrame on screen when it loads up? Im using netbeans 6.0.
i dont think there is a way
I Use SetLocation method using 2 parameter width & height
I tried this but it didnt work:
Where should I put the code and what do I need to change in order for it to work?Code:this.setLocation(this.WIDTH,this.HEIGHT);
Thanks for your help
Code:this.setLocation(300,200);
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();
}
Yes maybe i always use setLocation Not only for the Frame but all other elements because i use JCreator I can only Code !
Try this code, better performance:
Code:Dimension screenResolution = this.getToolkit().getScreenSize();
this.setLocation((screenResolution.width - this.getWidth()) / 2, (screenResolution.height - this.getHeight()) / 2);