So I'm pretty new to GUI, and came across the problem that whatever I set my preferred size to ( via setPreferredSize(new Dimension(width, height))
) it adds 10
to both width and height. I was wondering, why does this happen? Yes I understand that it is "preferred size" but surely there is a way to prevent this. I can fix it by simply subtracting 10
from width and height before passing it to the method, but I was wondering what was going on.
My (relevant) Code:
Main method:
public static void main(String[] args)
{
final App app = new App(500, 500);
JFrame frame = new JFrame("Block Stacker 3000");
frame.getContentPane().add(app);
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
System.out.println(app.getSize());
app.start();
}
App constructor (extends JPanel):
public App(int width, int height)
{
this.width = width;
this.height = height;
System.out.println(width + " " + height);
setPreferredSize(new Dimension(width , height));
setBackground(Color.CYAN);
}
When run, it outputs the following:
500 500
java.awt.Dimension[width=510,height=510]
Also, I've tried using setMinimumSize()
and setMaximumSize()
but neither really affected anything.