How to layout? (JFrame, JPanel, etc.)

后端 未结 1 2021
一向
一向 2020-12-20 08:30

I\'m very new to Java Swing and Java overall (my class just got finished on Scanner and basics). I was taught only Swing basics which is \"Wha

相关标签:
1条回答
  • 2020-12-20 09:02

    A common strategy to solve complex computing tasks, is to break them into small, well defined manageable tasks. Divide and conquer.
    This also applies to gui: break the design into small, easy to layout containers. For example:

    You can see four fairly simple and distinct containers, named headerPane, listPane, inputPane and buttonsPane. The mainPane just warps (contains) those four.
    The inputPane area is divided into containers, to keep the layout simple.

    The idea is to keep each container layout simple, easy to follow and change.
    headerPane can be as simple as:

    JPanel headerPane = new JPanel(); //uses flow layout by default
    JLabel header = new JLabel("LUNA BOOKSTORE ORDER FORM", JLabel.CENTER);
    headerPane.add(header);
    

    buttonsPane can be as simple as:

    JPanel buttonsPane = new JPanel(); //uses flow layout by default
    buttonsPane.add(new JButton("CONFIRM"));
    buttonsPane.add(new JButton("RESET"));
    buttonsPane.add(new JButton("EXIT"));
    

    More examples of applying this strategy: 1 2 and 3

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