Responsive Ad Area

Share This Post

test

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");  
    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?


Set the size of visible space in a JFrame
Set the size of visible space in a JFrame
test
{$excerpt:n}

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