The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.awt  [78 examples] > The Screen  [8 examples]

e599. Centering a Frame, Window, or Dialog on the Screen

    // Get the size of the screen
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    
    // Determine the new location of the window
    int w = window.getSize().width;
    int h = window.getSize().height;
    int x = (dim.width-w)/2;
    int y = (dim.height-h)/2;
    
    // Move the window
    window.setLocation(x, y);

 Related Examples
e598. Getting the Screen Size
e600. Getting the Number of Screens
e601. Enabling Full-Screen Mode
e602. Double-Buffering in Full-Screen Mode
e603. Getting the Available Screen Sizes, Refresh Rates, and Number of Colors
e604. Getting the Current Screen Refresh Rate and Number of Colors
e605. Setting the Screen Size, Refresh Rate, or Number of Colors

See also: Colors    Components    Containers    Cursors    Drawing    Events    Focus    Frames    GridBagLayout    Images    Shapes    Simulating Events    Text   


© 2002 Addison-Wesley.