Adding JScrollPane to a JPanel without a Layout Manager

后端 未结 1 979
傲寒
傲寒 2021-01-24 03:35

Before I start, I\'m aware that its a bad idea to not use a Layout Manager and usually I do use one, however, I also have all my components automatically re-size and relocate ba

1条回答
  •  北恋
    北恋 (楼主)
    2021-01-24 03:57

    The problem is JScrollPanel needs the preferred size of the view component to determine when the view exceeds the scroll pane's viewable area.

    The preferred size of a component is normally determined via the layout manager. While you can use setPreferredSize, it is typically discouraged and you will simply run into the same problem as the content exceeds what ever value you decide to set.

    A better solution would be to use a LayoutManager or compound layout (using multiple layout managers over multiple containers) to achieve the result you desired...or write your own...

    0 讨论(0)
提交回复
热议问题