Java layout manager in-depth discussion

Source: Internet
Author: User
Java layout manager in-depth discussion
5/23/2001 16:59:54 · Liu Zhiyou · yesky
--------------------------------------------------------------------------------
We all know that the GUI definition of java is completed by awt class and swing class. It adopts the separation of container and layout management in layout management. That is to say, the container simply puts other small pieces in it, regardless of how these small pieces are placed. LayoutManager is used to manage the layout.
In fact, java should not succeed in GUI. The structure of Awt and swing classes is very complex. In addition, subclass inheritance and interface implementation during the flood process make it very difficult to master these two classes. This is also a lot of complaints from java programmers, but the GUI has become the direction of program development, so here we have to leave it difficult.
Now let's look at the specific implementation of the layout manager in java. As we have mentioned earlier, containers in java only add small pieces (Meta). That is to say, they only add small pieces to their own interior using their own add () method. At the same time, it records the number of small pieces added to it. You can use the container. getComponentCount () method class to obtain the number of small pieces, and use container. getComponent (I) to obtain the handle of the corresponding small pieces. Then the LayoutManager class can use this information to deploy the small parts.
Java provides several common layout manager classes, such as BorderLayout, FlowLayout, and GridBagLayout. However, in actual layout, we still have other requirements. I used to have a vertical streaming layout in a recent issue. I call it VflowLayout. In fact, BoxLayout and GridBagLayout can do similar work, but the former is a member of the swing class, my client is an applet and cannot be used. The latter must specify the number of columns when the class is generated, without flexibility. Therefore, I decided to rewrite my own layout manager. After analysis, all LayoutManager must implement an Interface, that is, LayoutManager Inerface or its sub-Interface LayoutManager2, which is used for complex layout management, such as GridCardLayout. LayoutManager has five methods to implement:
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 );
The first method is actually the method you call when using container. add (String name, component comp); for example, when BorderLayout is the layout manager. However, because there is no additional information in FlowLayout, you do not need to fill in this method. The second method does not need to be filled. The true core method is the third and fifth methods. The former determines the size of the Container, while the latter determines the actual location of each small part in the Container. That is to say, when we use container. after setLayout (LayoutManager) is added to the small component, the work done by the system is actually LayoutManager. layoutContainer (container); and container. setSize (LayoutManager. preferredLayoutSize (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.