CenteringaWindow.java Source code

Java tutorial

Introduction

Here is the source code for CenteringaWindow.java

Source

import java.awt.GraphicsEnvironment;
import java.awt.Point;
import javax.swing.JFrame;

public class CenteringaWindow {
    public static void main(String[] args) {
        JFrame aWindow = new JFrame("This is the Window Title");
        Point center = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
        int windowWidth = 400;
        int windowHeight = 150;
        // set position and size
        aWindow.setBounds(center.x - windowWidth / 2, center.y - windowHeight / 2, windowWidth, windowHeight);
        aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        aWindow.setVisible(true); // Display the window
    }
}