Java: Difference between the setPreferredSize() and setSize() methods in components
问题 Ok, I read the Java Documentation and I just can\'t figure out what is the main difference between those two methods. Sometimes I used setSize(), sometimes setPreferredSize(), sometimes one does what I want, sometimes the other. So, what is the main difference between the two? Which one should I use for JFrames and JPanels ? Thanks 回答1: The short answer is: it's complicated. The slightly longer answer is: use setSize() if your component's parent has no layout manager, and setPreferredSize()