Positioning the Frame On-Screen - Java Swing

Java examples for Swing:JFrame

Description

Positioning the Frame On-Screen

Demo Code

import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.JFrame;

public class HelloFrame extends JFrame {
  public static void main(String[] args) {
    new HelloFrame();
  }//w  w  w .  j a  v a2s . c o m

  public HelloFrame() {
    this.setSize(200, 100);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setTitle("Hello, World!");
    this.setVisible(true);

    Toolkit tk = Toolkit.getDefaultToolkit();
    Dimension d = tk.getScreenSize();
    int x = d.width / 2;
    int y = (d.height / 2) - this.getHeight();
    this.setLocation(x, y);

  }
}

Related Tutorials