14 (3pts) Briefly describe how to choose which method (setSize or setPreterredSi
ID: 3714518 • Letter: 1
Question
14 (3pts) Briefly describe how to choose which method (setSize or setPreterredSize) to use when sizing Java components Use setPreferredSize if the layout manager honors the component's size; use setSize if the layout manager does not honor the component's size Use setSize if the layout manager honors the component's size. Use setPreferredSize if the layout manager does not honor the component's size Use setSize for the outer container (i.e., when a component is not contained in another component), use setPreferredSize for inner containers or components Use setPreferredSize for the outer container (i.e., whena component is not contained in another component), use setSize for inner containers or components Use setSize for components that are not put in a layout manager, use setPreferredSize for components that are put in a layout manager Use setPreferredSize for components that are not put in a layout manager, use setSize for components that are put in a layout manager None of the above are correct. OExplanation / Answer
Ans: Option 1
Explanation:
use setSize() if your component's parent has no layout manager, and setPreferredSize() and its related setMinimumSize and setMaximumSize if it does.
setSize() probably won't do anything if the component's parent is using a layout manager; the places this will typically have an effect would be on top-level components (JFrames and JWindows) and things that are inside of scrolled panes. You also must call setSize() if you've got components inside a parent without a layout manager.
As a general rule, setPreferredSize() should do the "right thing" if you've got a layout manager; most layout managers work by getting the preferred (as well as minimum and maximum) sizes of their components, and then using setSize() and setLocation() to position those components according to the layout's rules. So (as an example) a BorderLayout will try to make the bounds of its "north" region equal to the preferred size of its north component - they may end up larger or smaller than that, depending on the size of the jframe, the size of the other components in the layout, and so on.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.