Swing: creating a JScrollPane that displays its component centered?

前端 未结 4 1235
别那么骄傲
别那么骄傲 2021-02-08 02:52

If you create a JScrollPane that has a viewport larger than the JScrollPane\'s component, it displays that component in the upper left.

Is there any way to change this b

4条回答
  •  终归单人心
    2021-02-08 03:25

    1. Put a JPanel into the scroll-pane
    2. Set the layout of the panel to a GridBagLayout.
    3. Put one component into the panel with no constraint. It will be centered.

    This is the technique used in the Nested Layout Example, which places the red/orange image into the center of the parent.

提交回复
热议问题