JFrame size is too small

Sam picture Sam · Oct 27, 2012 · Viewed 15.3k times · Source

i have created a JFrame in netbeans. But when i run the program, the Jframe size is too small. here is my code.

import javax.swing.JFrame;    

public class Window {

    private static void demo()
    {
        JFrame frame =new JFrame();
        frame.setVisible(true);
    }
    public static void main(String[] args) {
       javax.swing.SwingUtilities.invokeLater(new Runnable(){
      public void run()
      {
          demo();
      }
        });
    }
}

And the output window looks like this.

Answer

Dan D. picture Dan D. · Oct 27, 2012

You can use frame.setSize(width, height) in order to set its size or frame.setBounds(x, y, width, height) for setting both the location and size.

A better choice would be to call frame.pack() after you add some components to its content pane.