Java JFrame set appropriate size via pack()
import javax.swing.JFrame; import javax.swing.JButton; import java.awt.Container; public class Main { public static void main(String[] args) { JFrame frame = new JFrame("Adding Components to JFrame"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container contentPane = frame.getContentPane(); // Add a Close button JButton closeButton = new JButton("Close"); contentPane.add(closeButton); /*from w w w. jav a 2 s. c o m*/ frame.pack(); frame.setVisible(true); } }