From the sounds of what you are saying, I think you misunderstand what the dimensions of a normal (decorated) frame include.
A JFrame
consists of a window/frame (normally decorated with a border), a JRootPane
, which contains a JLayeredPane
which contains the content pane, JMenuBar
and glass pane.
This is, one of the, reasons why you should never override a top level containers paint
method, because you won't actually be painting within the content/view area.
So, the actual "paintable" region of a frame is it's width - border.width
x height - border.height
The red line indicates the frame, the blue indicates the content pane.
This raises a very important question, where's the center of the frame? From the frames perspective, it's 100x100, but from the content pane's perspective, it's 92x81. Depending on what you want, will depend on which value you will use. For positioning the frame, you will want to to use the frames center point, for painting, your will want to use the content panes.
Now, the easiest way to center a frame on the screen is simply to call Window#setLocationRelativeTo(null)
otherwise, I would suggest you use Timr's solution ;)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…