So I've been programming in java for a semester or so, and I've had this problem a few times and finally got around to asking.
If I make a JFrame
and then set the size, like setSize(400,800)
for example. The frame is not actually 800 pixels
long. From what I can tell it is actually more like 770 (or maybe 769) pixels
long. Also, if you set the vertical size very low (below 30), the frame doesn't even show up, only the top window bar from the OS and the frame doesn't get bigger until you go to a value over 30 (so setSize(400,0)
looks the same as setSize(400,20)
). Why is this, it's not hard to fix but its weird and I'm curious why this is?
If you need more information about anything just ask and I'll get it to you.
JFrame SetSize() contains the the Area + Border.
I think you have to set the size of ContentPane
of that
jFrame.getContentPane().setSize(800,400);
So I would advise you to use JPanel embedded in a JFrame and you draw on that JPanel. This would minimize your problem.
JFrame jf = new JFrame();
JPanel jp = new JPanel();
jp.setPreferredSize(new Dimension(400,800));// changed it to preferredSize, Thanks!
jf.getContentPane().add( jp );// adding to content pane will work here. Please read the comment bellow.
jf.pack();
I am reading this from Javadoc
The
JFrame
class is slightly incompatible withFrame
. Like all other JFC/Swing top-level containers, a JFrame contains aJRootPane
as its only child. The content pane provided by the root pane should, as a rule, contain all the non-menu components displayed by theJFrame
. This is different from the AWT Frame case. For example, to add a child to an AWT frame you'd write:
frame.add(child);
However using
JFrame
you need to add the child to theJFrame
's content pane instead:
frame.getContentPane().add(child);