Pack in Swing

import java.awt.Container;
//  w  ww  .  j a v a 2 s  .co m
import javax.swing.JButton;
import javax.swing.JFrame;

public class Main {
  public static void main(String[] args) {
    JFrame frame = new JFrame("JFrame");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Add a close button
    JButton closeButton = new JButton("Close");
    Container contentPane = frame.getContentPane();
    contentPane.add(closeButton);

    // Calculates and sets appropriate size for the frame
    frame.pack();

    frame.setVisible(true);
  }
}
Yasna Aminaei