Centering the Screen

Introduction

This example shows how a Swing Window Object  can be centered on the screen. Note that the Window Object doesn’t have a CENTER method of its own so you need to implement the logic yourself.

// 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);

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.