Center a Frame
Here is the code for centering a frame into the middle of the screen. In this example, the frame name is "frame".

Here is the sample code for a classfile named TestFrame.class which has code to show a frame inside, plus code to center the frame into the middle of the screen:

import java.awt.*;
public class TestFrame extends Frame
  public TestFrame()
  public static void main(String[] args)
    TestFrame frame = new TestFrame();;
    //Center frame
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension size = frame.getSize();
    screenSize.height = screenSize.height/2;
    screenSize.width = screenSize.width/2;
    size.height = size.height/2;
    size.width = size.width/2;
    int y = screenSize.height - size.height;
    int x = screenSize.width - size.width;
    frame.setLocation(x, y);
admin / Dec 09, 2003
2004 - 2019 /