Swing Java Tutorial

The pack method resizes a JFrame to a default width and height.
To resize a JFrame, call the setSize method

public void setSize (int width, int height)
public void setSize (java.awt.Dimension d)
The setBounds method allows you to set the size as well as the new top-left corner of the JFrame

public void setBounds (int x, int y, int width, int height)
The setLocationRelativeTo method allows you to set your Swing component's location relative to another component.

public void setLocationRelativeTo (java.awt.Component component)
If the component is not visible or if you pass null, your JFrame will be display on the screen center.

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class ResizingPositioningJFrame {
  public static void main(String[] args) {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setTitle("JFrame Test");
    frame.setLayout(new GridLayout(3, 2));
    frame.add(new JLabel("First Name:"));
    frame.add(new JTextField());
    frame.add(new JLabel("Last Name:"));
    frame.add(new JTextField());
    frame.add(new JButton("Register"));
    int frameWidth = 200;
    int frameHeight = 100;
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    frame.setBounds((int) screenSize.getWidth() - frameWidth, 0, frameWidth, frameHeight);
    frame.setVisible(true);
  }
}