Let's clarify a few things. A LayoutManager used by a Container (such as Frame , Panel or Applet ) to calculate the position and size of components inside a Container . This means that it is wrong to talk about "nesting LayoutManager s". On the other hand, you can embed the Container into each other and give each one its own LayoutManager . I believe that this is what you want to do.
Let me illustrate this with a far-fetched example:
public class MyGUI { public static void main(String[] args) { Frame f = new Frame("Layout Example"); Panel mainPanel = new Panel(new BorderLayout()); f.add(mainPanel); Panel toolBar = new Panel(new FlowLayout()); toolBar.add(new Button("Button 1")); toolBar.add(new Button("Button 2")); mainPanel.add(tollBar.NORTH); Panel statusBar = new Panel(new FlowLayout()); statusBar.add(new Label("Status")); mainPanel.add(statusBar); f.pack(); f.show(); } }
Note that you need to create a new Panel for each LayoutManager . Rather, every Panel you create needs a LayoutManager . In addition, this example can easily be changed from AWT to Swing, replacing Frame with JFrame , Panel with JPanel , Button with JButton and Label with JLabel .
ps The above code is not verified. However, this should illustrate the concepts presented here.
source share