title: How to Set JPanel's Width and Height?
body: <p>I'm developing Snake game using Java. Board's (where all action takes it place) width and height should be fixed (640 pixels * 480 pixels).</p>
  
  <p>Structure:</p>
  
  <ul>
  <li><em>Main class</em> that extends from <em>JFrame</em> class,</li>
  <li><em>Board</em> class that extends from <em>JPanel</em> class;</li>
  </ul>
  
  <p>In <em>Main</em> class I have something like...</p>
  
  <pre><code>setSize( 1024, 768 );
  </code></pre>
  
  <p>...and in Board class I have...</p>
  
  <pre><code>setSize( BOARDS_WIDTH, BOARDS_HEIGHT );
  </code></pre>
  
  <p>Problem is that main and only windows seems to be like 1024 * 768, but panel inside it - not like 640 * 480. I have <em>setBackground()</em> as well and background is filled more than 640 * 480.</p>
  
  <p>Is there any way to make Board's width and height like 640 * 480, but main windows width and height - as much as Board's width and height are correct?</p>
  
  <p>Right now it looks like this...</p>
  
  <p><img src="https://i.stack.imgur.com/Rur4C.jpg" alt="Width and height are ~1000 * ~750, not 640 * 480."></p>
  
  <p><strong>Edit:</strong></p>
  
  <p>All works... almost.</p>
  
  <p><em>Main</em> class:</p>
  
  <pre><code>add( new Board(), BorderLayout.CENTER );
  pack();
  setResizable( false );
  setLocationRelativeTo( null );
  setVisible( true );
  setDefaultCloseOperation( EXIT_ON_CLOSE );
  setTitle( "Snake (by daGrevis)" );
  </code></pre>
  
  <p><em>Board</em> class:</p>
  
  <pre><code>setPreferredSize( new Dimension( 640, 480 ) );
  </code></pre>
  
  <p>Gray background is 642 * 482! Like there would be border that wraps all by 1 pixel!</p>