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

☆樱花仙子☆ 提交于 2019-12-06 15:31:10

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"));

You can see more examples of applying this strategy here and here.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!