简体   繁体   中英

How do I determine the minimum width of a JFrame to hide the horizontal scrollbar?

When I create a JFrame with child components, it opens to the smallest possible width a window can be, when I expected the width to be the minimum size to display the contents of the window without horizontal scroll bars.

How do I determine what that minimum size should be? Obviously something in the UI knows, because when made wide enough, the horizontal scroll bar disappears. This is the state I want the window when created. How do I fetch that number, which I suspect I would return as an override to getPreferredSize? As far as a preferred height, that's arbitrary.

As much as I know, when you use the pack() method, it automatically chooses the minimum size needed for all the components in-hand.

In another question, this subject is covered:

The pack method sizes the frame so that all its contents are at or above their preferred sizes. An alternative to pack is to establish a frame size explicitly by calling setSize or setBounds (which also sets the frame location). In general, using pack is preferable to calling setSize, since pack leaves the frame layout manager in charge of the frame size, and layout managers are good at adjusting to platform dependencies and other factors that affect component size.

From Java tutorial

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM