How can I specify the initial position and size of a Frame/JFrame?

Simon Brown

To set the size on a frame (or JFrame), use the setSize() method, passing either a Dimension object, or the width and height values in pixels. For example :

myFrame.setSize(640, 480);

Another (and IMHO better) way of sizing a window is to use the pack() method. This sizes the window so that each of its components are visible at their preferred size.

To set the initial position for a frame (or JFrame), use the setLocation() method specifying a Point object, or the x and y coordinates. The top-left of the screen is at 0,0.

myFrame.setLocation(0, 0);

These along with other useful methods are defined on java.awt.Component class.

The java.awt.Toolkit class contains the getScreenSize() method for getting the size of the screen - just in case you wish to make the window the full size of the screen or center your window.

0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact