Why will BoxLayout not allow me to change the width of a JButton but let me change the height?

前端 未结 6 1836
别那么骄傲
别那么骄傲 2020-12-31 01:49

I\'m trying to get the Layout of a JDialog of mine to fit a particular look that a program in which I\'m porting to Java has, I\'ve used several LayoutManagers

6条回答
  •  借酒劲吻你
    2020-12-31 02:36

    I know this is an old question but I don't really see a good explanation. So for the sake of searchers that stumble upon this I will add my two cents.

    There are three methods associated with sizing components in Swing: setPreferredSize(), setMinimumSize(), and setMaximumSize(). However, the important point is that it is up to the particular layout manager being used as to whether or not it honors any of these methods.

    For BoxLayout (the layout the original poster is using):

    • setMinimumSize() -- BoxLayout honors this
    • setMaximumSize() -- BoxLayout honors this
    • setPreferredSize() -- if X_AXIS is being used width is honored, if Y_AXIS is being used height is honored

    The OP is using a Y_AXIS BoxLayout which is why only his height was being changed.

    Update: I put together a page with this same information for all of the layout managers. Hopefully it can help some searchers out: http://thebadprogrammer.com/swing-layout-manager-sizing/

提交回复
热议问题