As we all know, the Java GUI interface definition is done by the AWT class and swing class. It uses the container and layout management scheme to separate the layout management. In other words, the container just put the other pieces into it, regardless of how these pieces are placed. The management of the layout is assigned to a specialized Layout manager class (LayoutManager) to complete.
In fact, Java in the GUI aspect should not be successful. The structure of the AWT and swing classes is very complex, combined with subclass inheritance and interface implementations, making it difficult to master these two classes. That's what a lot of Java programmers are complaining about, but the GUI has become the direction of the program, so here we have to be reluctant.
Now let's look at the concrete implementation of the layout manager in Java. As we mentioned earlier, container classes in Java (Container), they just add small pieces (Meta), that is, it uses its own add () method to add small pieces to themselves. At the same time, he recorded the number of small pieces added to its interior, the Container.getcomponentcount () method can be used to obtain the number of small pieces, through container.getcomponent (i) to obtain the corresponding small handle. The LayoutManager class can then be used to actually lay out small pieces of the information.
Java has provided us with several common layout manager classes, such as BorderLayout, FlowLayout, GridBagLayout, and so on. But in the actual layout, we will still have other needs. In a recent question I used to have a vertical flow layout, I call vflowlayout, in fact, BoxLayout and GridBagLayout can do similar work, but the former is a swing class member, my client is an applet, can not be used, The latter had to specify the number of columns when the class was generated, and the flexibility was lost, so I decided to rewrite my own layout manager to implement it. After analysis, all layoutmanager have to implement an interface, that is, LayoutManager Inerface or one of his sub-interfaces LayoutManager2 Interface, which is used for complex layout management, such as Gridcardlayout. LayoutManager has five methods that need to be implemented, namely:
1. public void Addlayoutcomponent (String name, Component comp);
2, public void Removelayoutcomponent (Component comp);
3, Public Dimension preferredlayoutsize (Container Container);
4, Public Dimension minimumlayoutsize (Container Container);
5, public void Layoutcontainer (Container Container);