Java - AWT/Swing

center window

java.awt.Window
In Java, you can center a window (JFrame, JDialog, …) with the method setLocationRelativeTo(Component c).
With it, the parameter indicates c, to what paramount window it shall be centered. The parameter c can also be zero. In this case, it is centered on the screen:


package com.sowas.javawiki;
 
import java.awt.*;
import javax.swing.*;
 
public class CenterFrame extends JFrame {
    public CenterFrame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);  // Here, the window is centered on the screen
    }
 
    public static void main(final String[] args) {
       new CenterFrame().setVisible(true);
    }
}

Eigene Werkzeuge
Werkzeuge

gratis Counter by GOWEB
seit 9.10.2007