Concrete implementation of Java layout Manager

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.