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
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);