Packing All Components of a JFrame to make a proper size - Java Swing

Java examples for Swing:JFrame

Description

Packing All Components of a JFrame to make a proper size

Demo Code

import java.awt.Container;

import javax.swing.JButton;
import javax.swing.JFrame;

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

    // Add a close button
    JButton closeButton = new JButton("Close");
    Container contentPane = frame.getContentPane();
    contentPane.add(closeButton);//from  ww w .  ja v a 2s  . c o m

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

    frame.setVisible(true);
  }
}

Related Tutorials