swing - JFrame not displaying the real size

Im very new to java. I don't know what's wrong with my frame. I set the size to 300 and 200. What Im seeing is a short and fat stick like thing.

Below is my code:

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;

import javax.swing.JFrame;

public class BicycleDemo extends JFrame {
    /**
     * The serialVersionUID.
     */
    private static final long serialVersionUID = -4541236176053545919L;

    public static void createGUI () {
            JFrame jFrame = new JFrame("JFrame Demo");
            jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            Container container = jFrame.getContentPane();
            container.setLayout(new FlowLayout());
            container.setBackground(Color.BLACK);

            jFrame.setSize(300, 200);
            jFrame.setResizable(false);
            jFrame.pack();
            jFrame.setVisible(true);
    }

    public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                 public void run() {
                      createGUI ();
                 }
            });
    }
}

Please help.

Answers


You are calling pack(). pack() method resizes the frame to the smallest possible size to hold all the elements. So in fact you set the size to 200 x 300 and then resize the frame once more with pack().

Be aware however, that "hold all elements" is calculated by their preferred size, which can be just 0x0 pixels in a lot of cases.


Need Your Help

How to enable auto format code in eclipse?

java jsf java-ee xhtml eclipse-juno

I have tried to format xhtml code in eclipse IDE - Juno using the shortcut by two ways. which are

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.