Search code examples
javaswingsizejframe

How do I make a JFrame a certain size, not including the border?


I have a JFrame that I've set to a certain size, using setBounds. However, that makes the window, including the borders, that size (which in hindsight makes complete sense). But what I want is for the size of the window it be, say, 800x600 plus the borders. This is important because I'm drawing to a Graphics object from the BufferStategy from the JFrame, but I've been drawing underneath the title bar when using a y value of less than about 20. I imagine different OSs (or even different OS settings) can have different thickness borders, too. I thought the borders were just tacked on a window afterwards, but this doesn't seem to be the case.

So, how do I make the space inside the borders a certain size, regardless of the thickness of the borders? Also, to make my life easier, how do I make point 0, 0 be the top left corner of the viewable contents of the frame?

By the way, using setUndecorated presents it's own problems, so I'm not trying that at the moment.


Solution

  • This is important because I'm drawing to a Graphics object from the BufferStategy from the JFrame,

    Why are you using a BufferStrategy. That is old AWT code. Swing is double buffered by default.

    When doing custom painting in Swing you should extend JPanel (or JComponent) and then override the paintComponent() method. You add this component to the content pane of the frame. Then if you follow kleopatra's advice you won't have a problem.

    See the section from the Swing tutorial on Custom Painting for more information and examples.