If I set the size of JPanel first, then set the panel to be the content pane of an unsized frame, the frame will be squeezed into a very small rectangle in the top-left corner of the screen.
But if I set the size of frame first and then set the panel to be its content pane the frame will be properly drawn.
Why does this happen and how do I solve this if I really want to specify the size of JPanel rather than JFrame?
You should tell the frame to pack itself when it is first shown:
This will cause the frame to adopt a size that suits its contents.