Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the JFrame setSize() method not set the size correctly?

Tags:

java

swing

jframe

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.

like image 835
convergedtarkus Avatar asked Jul 06 '11 08:07

convergedtarkus


People also ask

Which class defines setSize () method?

setSize() is a method of Vector class that is used to set the new size of the vector.

What is the use of setSize () method?

Description. The setSize(int newSize) method is used to set the size of this vector. If the new size is greater than the current size, new null items are added to the end of the vector. If the new size is less than the current size, all components at index newSize and greater are discarded.

How do I fix the size of a JFrame?

You can change the size of the JFrame by simply placing the cursor in the corners and dragging it. Or if you press the resize option next to close(X) in the upper right corner, it will be enlarged to full-screen size. This happens because the resize is set to “true” by default.

What happens if setSize () is not called on a JFrame?

What happens if setSize is not called on a window? 1) The window is displayed at its preferred size. 2) It is a syntax error. 3) The window is not displayed.


1 Answers

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 with Frame. Like all other JFC/Swing top-level containers, a JFrame contains a JRootPane as its only child. The content pane provided by the root pane should, as a rule, contain all the non-menu components displayed by the JFrame. 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 the JFrame's content pane instead:

frame.getContentPane().add(child);

like image 115
Talha Ahmed Khan Avatar answered Oct 29 '22 16:10

Talha Ahmed Khan