Multiple Layout Managers in Java

前端 未结 2 615
南旧
南旧 2020-12-20 16:35

Is there way to use more than 1 layout manager in Java. Right now I\'m using a gridLayout to implement a chess board but beneath it I would like to put some other stuff but

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-20 16:59

    Is there way to use more than 1 layout manager in Java.

    Absolutely. In fact, using multiple layout managers is the norm.

    How would I go about doing this?

    Any Container subclass can have a LayoutManager and contain child elements. And each of these child elements can itself be a Container with children. The most commonly used container below the top-level frames is JPanel.

    For your example, you should probably use a BorderLayout for the frame, put a JPanel with the grid in its CENTER position (because that's the one that gets all available remaining space when the other positions have been given their preferred sizes) and another JPanel with the "other stuff" in the SOUTH position.

    More details can be found in the Swing tutorial on layout managers.

提交回复
热议问题