Ricardo
Ricardo

Reputation: 355

Why should I use the awt.Dimension class?

I'm trying to create a Java swing GUI. One thing I came across is when setting the size of a JFrame instance, you can use frame.setSize(new Dimension(int,int)); to set the size of the JFrame. Why not set the size doing frame.setSize(int, int);?

What's the difference and which is better?

Upvotes: 0

Views: 50

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

Your dichotomy is a false one since it doesn't matter in this situation which JFrame#setSize(...) method you use, one that uses ints or Dimension.

No the real issue here is a Swing-specific issue, that you shouldn't be setting size at all, but rather should be overriding

public Dimension getPreferredSize() {
   // ...
}

or better still, let your components size themselves in a smart way, and allowing the JFrame#pack() method do its thing -- tell the layout managers to lay out components, and to tell components and containers to properly size themselves. There are many reasons for this, but mainly it is so that you don't mis-size your components accidentally.

Upvotes: 2

Related Questions