With the rapid development of Internet, Java technology has become more and more widely used. No matter we use J2SE, J2EE, or J2EE, the GUI cannot be avoided. Nowadays, more and more application software requires User-friendly interfaces, powerful functions, and easy to use. As we all know, the implementation of GUI Design in Java is much more complicated and troublesome than its cross-platform and multi-thread features. This is what many Java programmers complain about. However, GUI has become the development direction of the program. Therefore, we must understand the GUI Design Methods and features of Java. In fact, using the layout Manager interface provided by Java and the corresponding layout management class, we can also make a very beautiful interface, of course, implementation is certainly much more troublesome than VB. This article attempts to introduce some specific application examples through my own development experience, hoping to help those who have been bored like me.
Layout manager in Java
Why layout?
In actual programming, each time we design a form, we need to add several components to it. To manage the layout of these components, we need to use the layout manager. For example, you can design a simple calculator or a text editor. Are these components arbitrarily arranged by the JVM? Or should we make a standard arrangement based on a certain positional relationship? Of course you should select the latter. Place the components added to the container in a certain order and rules to make them look more beautiful. This is the layout. In Java, LayoutManager manages the layout. So when should we use the layout manager? Which layout manager should be selected? How can I use the layout manager?
If you are writing a GUI program, you should not hard set the size and position of the component when using the AWT/Swing component, but use the LayoutManager of Java) to set and manage the size and position of the visual component. Otherwise, layout confusion may occur. Do not believe it. You can create a new Frame (or JFrame) and add several buttons (or jbuttons) to it using the setBounds () method. Once you expand or contract the form, you will find that the components are not arranged exactly as you expected. To solve this problem, when the form (or container) is scaled, the component position is adjusted properly, so we need to use the layout manager.
To this end, we must first know the layout method of Java, what layout managers are in the APIS provided by Java, and what are their layout features.
Java Layout
We all know that the GUI definition of Java is completed by AWT class package and swing class package. It adopts the separation of container and layout management in layout management. That is to say, the container simply puts other components into it, regardless of how these components are placed. Layoutmanager is used to manage the layout.
Now let's look at the specific implementation of the layout manager in Java. As we have mentioned earlier, containers in Java only add components. That is to say, they only add components to their internal systems using their own add () method. At the same time, it records the number of components added to it. You can use the container. getcomponentcount () method class to obtain the number of components and use container. getcomponent (I) to obtain the corresponding component handle. Then, the layoutmanager class can use this information to deploy components in the layoutmanager class.
Java provides several common layout manager classes, such as flowlayout, borderlayout, gridlayout, and gridbaglayout. The following lists their layout features:
Package features
Java. AWT cardlayout places the component in the container like a card, and only one component is visible at a time.
Java. AWT flowlayout sorts components from left to right and from top to bottom in sequence. If one row cannot be filled, It is folded to the next row and then placed
Java. AWT gridlayout is similar to a frameless table. Each cell contains a component.
Java. AWT borderlayout places components in the east, south, west, north, and middle regions. Only one component can be placed in each direction.
Java. AWT gridbaglayout is flexible. You can specify the position of a component and the number of cells occupied.
Javax. Swing boxlayout is like a neatly placed row or column of boxes. Each box contains a component.
Javax. swing SpringLayout place child components based on a set of constraints
Javax. swing ScrollPaneLayout is dedicated to JScrollPane and contains a Viewport, a row header, a column header, two scroll bars, and four corner components.
Javax. swing OverlayLayout overlays components in the form of mutual coverage.
Default layout manager of Javax. swing ViewportLayout JViewport
In fact, in most cases, the integrated use of these layout managers can meet the needs. Of course, for special applications, we can define our own layout manager by implementing the LayoutManager or LayoutManager2 interface. The following describes how to use common layout managers through several instances.