Here's a self-contained test case, based on your code:
import java.awt.*;
import javax.swing.*;
public class Test {
public PaintPanel paintPanel;
public JFrame frame;
public Test() {
frame = new JFrame("Sheepness simulation");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setSize(width, height);
BorderLayout frameLayout = new BorderLayout();
JPanel background = new JPanel(frameLayout);
background.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
paintPanel = new PaintPanel(this);
paintPanel.setPreferredSize(new Dimension(320, 240));
background.add(BorderLayout.CENTER, paintPanel);
frame.getContentPane().add(background);
frame.pack();
frame.setResizable(false);
frame.setVisible(true);
}
public static class PaintPanel extends JPanel {
public Test window;
public PaintPanel(Test window) {
this.window = window;
}
@Override
public void paintComponent(Graphics g) {
g.setColor(Color.blue);
g.fillRect(0, 0, 320, 240);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Test();
}
});
}
}
When I run it on an IcedTea JVM on Linux, I see this.
Your problem is either due to the button container forcing the window to be wider, or possibly due to a Swing bug in the version of Java you're using.
My recommendation is not to bother with the built-in layout classes and to use MigLayout instead.