1

I was originally using setSize but this resulted in the on screen content being slightly bigger than the screen due to java borders and title room. So I used setpreferredSize and now the screen size is slightly too big. leaving a space around the right and bottom sides of my content.

In my JFrame:

add(new Board());
    setTitle("Rougebot");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    //setSize(600, 800);
    //getContentPane().setPreferredSize(new Dimension(600,800));
    pack();
    setLocationRelativeTo(null);
    setVisible(true);
    setResizable(false);

In my Board (JPanel)

setPreferredSize(new Dimension(600,800));
SeanBing
  • 33
  • 7
  • I made a test from your example (here: http://pastebin.com/AV1pG4BH), where the `JFrame` has only your code in the constructor, and `Board` is a `JPanel` with just `setPreferredSize` and `setBackground(Color.BLACK)` in the constructor, and it look fine (on Windows 7 with classic theme, at least). Is there some other relevant properties you are setting? What platform are you on? – Jason C Nov 02 '13 at 09:24
  • windows 7 mate. I ran your code it comes up with 600x800, and I ran mine and system output came up with 600x800. However I have added images that are 600px long and start at 0px and there is still a gap. – SeanBing Nov 02 '13 at 10:22

2 Answers2

8

Call setResizable(false) before calling pack():

add(new Board());
setTitle("Rougebot");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false); // <-- here
pack();
setLocationRelativeTo(null);
setVisible(true);

And continue setting the preferred size of Board, as in the example in your original post.

In Windows, fixed borders are thinner than resizable ones, if you compute sizes then set to fixed size, you change the window border width, which, in Windows, causes the client area to increase and the outer dimensions to remain fixed.

Jason C
  • 38,729
  • 14
  • 126
  • 182
  • 4
    You absolute beast of a programer. This solved it. I would upvote you but I apparently don't have enough rep yet. But seriously, thanks! – SeanBing Nov 02 '13 at 10:42
  • @Jason C, sorry if i'm bothering you, i know it's a wrong place to post that, but i stopped my work for 2days looking for a way for this https://stackoverflow.com/questions/54507359/how-to-resize-the-jframe-content-according-to-the-jframe-size-correclty – M J Feb 04 '19 at 16:39
  • Brilliant answer. It helped me to solve another problem. For me the .pack() method made the borders smaller than I set in the setPreferredSize() (even smaller than the borders of the JPanel previously added to JFrame) and I could not understand why. Many thanks! – Alexander Samoylov Mar 16 '19 at 11:27
  • 2
    I would like to mention in addition that in order to resolve my issue completely I had also to replace myJFrame.setPreferredSize() with myJFrame.getRootPane().setPreferredSize or myJFrame.getContentPane().setPreferredSize(). With the combination of the above mentioned solution from Jason C I reached the completely proper dimensions for my JFrame window. – Alexander Samoylov Mar 16 '19 at 14:02
1

Frames have decorations (the outer frame). What pack tries to do is make sure that the content (viewable) area meets the requirements of the preferred size of all the child components and then ADDs the frame decoration around it.

The size of the decoration is platform depended

For example...

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Okay but when I use preferred size, the content area is now too big. It should be 600px across for example but it's more like 620 or something. This means I have an annoying gap around the right side of the content. Same for height as well – SeanBing Nov 02 '13 at 10:14