JPanel size by inner components

前端 未结 8 2204
夕颜
夕颜 2021-02-18 13:48

Is it possible to tell JPanel to set its size to fit all components that it contains? Something like pack() for JFrame.

edit: The trick with preferredSize d

8条回答
  •  借酒劲吻你
    2021-02-18 14:31

    By default Containers have a preferred size that matches the preferred layout size given by the container. So literally all you have to do is:

    panel.setSize(panel.getPreferredSize());
    

    Presumably you are doing something odd with the parent to stop the parent's layout manager doing the equivalent of this.

提交回复
热议问题