2

My question is very wierd, after searching and reading the code 40 times, i can't explain why the jpanel and jframe gets additional 10 pixels to their width and height.

the code is:

public class Game extends JPanel implements Runnable {

        public static final String NAME = "ALPHA !";

        public static final int WIDTH = 600, HEIGHT = 400;

        public static void main(String[] args) {
            Game game = new Game();
            game.setMinimumSize(new Dimension(WIDTH, HEIGHT));
            game.setMaximumSize(new Dimension(WIDTH, HEIGHT));
            game.setPreferredSize(new Dimension(WIDTH, HEIGHT));
            game.setFocusable(true);
            game.requestFocusInWindow();

            JFrame frame = new JFrame(NAME);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            frame.setLayout(new BorderLayout());
            frame.add(game, BorderLayout.CENTER);
            frame.pack();

            frame.setResizable(false);
            frame.setAlwaysOnTop(true);
            frame.setLocationRelativeTo(null);

            frame.setVisible(true);
            game.startGame();
        }

        public void startGame() {
            System.out.println("w " + getWidth() + ", h " + getHeight());
            new Thread(this).start();
        }
}

The println in the startGame method prints:

w 610, h 410

Thanks in advance.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Orel Bitton
  • 547
  • 1
  • 6
  • 11
  • 4
    This is (not very well know) issue with using `setResiabled`, check [this answer](http://stackoverflow.com/questions/13506955/jframe-isresizablefalse-sizing-issue) for more details. Also, a few things come to mind- firstly, don't try and set the size of the frame to a static size, instead, set the preferred size of it's content to the size you want and allow the frame to fill around it. Frames have borders which take up space, so using (for example) 600x400 may only result in a usable area of 590x395 (or an area less then you specified) – MadProgrammer Jul 06 '13 at 21:53
  • @MadProgrammer Thanks for the answer you have linked me to, i've found that i should pack after setResizable(). – Orel Bitton Jul 06 '13 at 21:58
  • Don't set the size of top level containers. Instead layout the content & call `pack()`. – Andrew Thompson Jul 07 '13 at 03:48
  • 3
    @AndrewThompson True, but if you call `setResizable` and then `pack`, it will still add 10 pixels to the frame :P – MadProgrammer Jul 07 '13 at 05:59

2 Answers2

0

Use frame.setUndecorated(true) then you will understand why there are 10pxls increased in the height and width.

Azad
  • 5,047
  • 20
  • 38
  • 1
    I don't think it will make a difference, there is a suitable bug in the API, that if you make a window unsizable, decorated or not, it adds 10 pixels to the window size. Took me forever to diagnose it :P - besides, a frames border is added into the window, not on to it – MadProgrammer Jul 07 '13 at 01:50
  • @MadProgrammer: I thought that disables or enables decorations for this frame makes this different, this is true only if the resizible is `true`, but after your comment, I figured out I made a mistake, thanks and don't need a negotiation because you're right :). – Azad Jul 07 '13 at 05:57
  • @AndrewThompson: I admitted this as a poor answer comes from lack of information. – Azad Jul 07 '13 at 06:08
  • 1
    And that -1 comes form leaving the poor answer, instead of *making it* into a comment.. – Andrew Thompson Jul 07 '13 at 06:10
  • @AndrewThompson: I wish I got more downvotes to not repeat this again, so, in this case I will say thanks for the downvote. – Azad Jul 07 '13 at 06:15
0

I just encountered this problem when trying to add a single custom JPanel to my JFrame, which acts as a canvas. I'm using Java 8 (update 231) and this is still an issue happened.

I don't exactly know why this is happening. In the end, the issue could be fixed by calling the frame.setResizable(false) after frame.setVisible(true) on the JFrame.

Since I'm late to the party here, I hope this will help someone struggling with this as well.

Have a nice day

EDIT: Just saw the comments below the answer suggesting kind of the same by moving frame.setResizable(false) before frame.pack(), which works as well. Sorry for bringing this up again.. :)

machinateur
  • 502
  • 5
  • 10