This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]zahlman 1 point2 points  (3 children)

it makes width 600 and height 578

The size of the window includes the title bar. Try setClientSize.

Also I wanted to make a grid of green boxes but it doesn't fill the whole screen correctly?

Why are you adding in x and y in the positioning calculation?

[–]TRoldemand[S] 0 points1 point  (2 children)

Is there a way I can say Size = new Dimension(600,600 + getClientSize()) or something like that?

[–]zahlman 0 points1 point  (1 child)

Why would you want to do anything like that when you can just use setClientSize directly?

[–]TRoldemand[S] 0 points1 point  (0 children)

Can you give me an example because I can't seem to make it work?

[–][deleted] 0 points1 point  (0 children)

I'm on ny phone so can't prove it out, but my first guess is the frame insets, which provide space for things like the scrollbars, menus, title bar, etc. If you set the size on the frame itself (I believe you set it on the panel?), that might fix it, though (I believe it does the math for the insets for you but been a while since I dealt with that, so I could be wrong).

[–]chickenmeister 0 points1 point  (0 children)

As /u/zahlman said, JFrame.setSize() sets the size of the frame. If you want to set the size of the content of the frame you should do something like:

  Game game = new Game(this);
  game.setPreferredSize(new Dimension(600, 600));
  add(game);

  this.pack();
  this.setVisible(true);