Responsive Ad Area

Share This Post


Set the size of visible space in a JFrame

Set the size of visible space in a JFrame

I have had this problem a few times before, and I finally attempted to solve it, but I cannot fond the right solution. I have a simple JFrame program. The basic JFrame code looks like this:

JFrame frame = new JFrame("halp");  

Here’s an image of what I’m trying to accomplish:


As you can see, there’s space taken up by the top bar and the borders of the window. One way to fix this is to increase the size of the window, but that isn’t neat at all, and also not every system has the same border sizes of the window.

I’ve done research and tried a few things I found on earlier asked questions:

  • frame.pack(). This method only set the window to minimal size, as if I didn’t set the size at all.
  • frame.getContentPane().setSize(500,500);. The window still has the borders counted in the frame size.
  • frame.setPreferredSize(new Dimension(500, 500)); and JPanel.setPreferredSize(new Dimension(500, 500));. The frame went into minimum size, as with frame.pack().

I tried combining these methods, but nothing seemed to work.

What am I missing? Is there another aspect of the JFrame I need to add? Is it an incompatibility issue of BlueJ?

Set the size of visible space in a JFrame
Set the size of visible space in a JFrame

Share This Post

Leave a Reply

Your email address will not be Publishedd. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Skip to toolbar