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");  
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500,500);
    frame.setResizable(false);
    frame.setVisible(true);

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

JFrame.

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?

1 Answers

1
TT. On Best Solutions
frame.getContentPane().setPreferredSize(new Dimension(500,500));

followed by a

frame.pack();

should solve your problem.