Doubt regarding dimensioning methods in a Frame

Asked

Viewed 66 times

1

What is the difference between the method setSize() and the method setPrefferedSize() in a Frame, and in which situation each one should be used?

  • It is interesting to always look for the documentation before, your last questions are answered in it.

1 answer

3


The method setPreferredSize() is inherited from the class Component, and as its name suggests, it defines a preferred size for the JFrame. Its use is recommended when we are making use of Layout Managers, for this is the way we inform him that we want the component to be, as far as possible, that size. Of course it can ignore this value depending on its functioning and the space required for the distribution of other components on the screen, and even due to the resolution of the monitor as well, but usually managers use this size as a base and respect it when rendering the component.

Other methods that can be used in conjunction with this to provide greater control of component size are the setMaximumSize() and setMinimumSize(), defining respectively the maximum and minimum size of the component.

Already the method setSize() sets window shape, manually, different from the previous method that only suggests a size to the layouts manager, but can also be ignored as the manager used. You can use it if the container of the component that sets the size does not have an active layouts manager, otherwise it can also be ignored.

As a rule, one should always use the first one, since not using a layout manager can bring much greater complications than managing the size of the components.

Sources:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.