Layout design of J2SE graphic interface

Source: Internet
Author: User
Tags joins

In the interface design, a container to place a number of components, in order to be beautiful, to arrange for components in the container position, this is the layout design. A variety of layout classes are defined in java.awt, and each layout class corresponds to a layout strategy. The following layout classes are commonly used: FlowLayout, placing components in turn. Boarderlayout, place the component on the boundary. CardLayout, the component is stacked like a poker, and only one of the components can be displayed at a time. GridLayout, the display area is divided into equal squares by rows and columns, and the components are placed in these squares in turn. GridBagLayout, divides the display area into many rectangular small units, each of which can occupy one or more small units.

Where GridBagLayout is able to perform fine position control and is the most complex, this tutorial does not discuss this layout strategy, which will be explained in detail in the feature article.

Each container has a layout manager that determines how to arrange the components that are placed inside the container. The layout manager is the class that implements the LayoutManager interface. I. FlowLayout layout (japplet,jpanel,jscrollpane default layout)

The FlowLayout layout is to arrange the components from left to right in the order in which they are joined, and then go to the line to continue from left to right, with the components in each row centered. This is the simplest layout strategy, generally used in the case of a few components, when the components are more, the components in the container will appear uneven, the length of the line varies.

FlowLayout is the default layout for small applications and panels, and the FlowLayout layout is constructed by: FlowLayout (), which generates a default FlowLayout layout. By default, the component is centered, with a gap of 5 pixels. FlowLayout (int aligment), sets the alignment of each honing component. The alignment value can be flowlayout.left,flowlayout.center,flowlayout.right. FlowLayout (int aligment,int horz, int vert), sets the alignment, sets the horizontal spacing of the components Horz and vertical spacing vert, and uses the Superclass container method SetLayout () to set the layout for the container. For example, code setlayout (New FlowLayout ()) sets the FlowLayout layout for the container. The way to join a component to a container is add (component name). two. BorderLayout layout (default layout for JWindow, Jframe,jdialog)

BorderLayout layout strategy is to divide the space inside the container into the east "West", "South" and "North", "Center" in five regions. When you join a component, you should indicate which area to put the component in. Put a component in one place. If you are adding more than one component to a location, you should first place the component that will join that location in another container, and then add the container to that location.

BorderLayout layouts are constructed in the following ways:
(1) BorderLayout () to generate a default BorderLayout layout. By default, there is no gap.
(2) borderlayout (int horz,int vert), sets the horizontal and vertical spacing between components.

The BorderLayout layout strategy is set by setlayout (new BorderLayout ()). The method of adding a component to a container is add (component name, location), and if no location is specified when the component is joined, the default is the medium position.

The BorderLayout layout is the default layout for JWindow, Jframe,jdialog.
The example 11-5 application has five tabs, which are placed in the east, west, south, north, and five regions of the window, respectively (view source files). three. GridLayout Layout

The GridLayout layout is a grid that divides the container into rows and columns, the number of rows and columns is controlled by the program, and the components are placed in small grids in the grid. The GridLayout layout is characterized by precise component positioning. Because each mesh in the GridLayout layout has the same shape and size, the components that require the container to be placed should also remain the same size.

GridLayout layouts are constructed in the following ways:
(1) GridLayout (), generates a single-column GridLayout layout. By default, no gaps are in progress.
(2) GridLayout (int row,int col), set a GridLayout layout with row row and column Col.
(3) GridLayout (int row,int col,int horz,int vert), sets the number of rows and columns of the layout, the horizontal and vertical spacing of the components.

The GridLayout layout acts as a benchmark, automatically increasing the columns when the number of components placed is exceeded, conversely, too few components automatically reduce the columns, the number of rows is the same, and the components are arranged in row-first order (automatically increment or decrement the column according to the component). Each grid of the GridLayout layout must be populated with components, and if you want a grid to be blank, you can replace it with a blank label (add (New label)).

The "Example 11-6" applet puts several buttons and several labels into jpanel, then puts JPanel into JScrollPane, and finally puts JScrollPane into the applet's window. The JScrollPane created by the program always has a horizontal and vertical scrollbar, and the visible range of the scrolling panel is smaller than the actual requirements of the panel, and you can move the slider of the scroll bar to display the area where the panel was not originally visible (view source file).

The GridLayout layout requires consistency in the size of all components, which may not look good with the interface. One way to remedy this is to have some widgets merged into a container and then put the container as a component and put it into the GridLayout layout. This is the container nesting that was mentioned earlier. For example, container a uses the GridLayout layout to divide the containers into meshes, and the containers B and C each add several components, and B and C are added as components to container a respectively. Containers B and C can also be set to GridLayout layouts, dividing themselves into several meshes, or set to other layouts. In this way, the size of each component varies from the perspective of its appearance. four. CardLayout layout

A container with a cardlayout layout can hold multiple components, but multiple components have the same display space, and only one component can be displayed at a time. Just like a stack of cards can only show the topmost one at a time, this displayed component will occupy the entire space of the container. CardLayout layout design steps are as follows:
Create a CardLayout layout object first. Then, use the SetLayout () method to set the layout for the container. Most, the Add () method of the call container joins the component to the container. The CardLayout layout strategy joins the component by:
Add (Component code, component);
Where the component designator is a string and is given separately, regardless of the component name.

For example, the following code sets the CardLayout layout for a JPanel container:
CardLayout Mycard = new CardLayout ();//create CardLayout Layout object
JPanel p = new JPanel ();//Create Panel Object
P.setlayout (Mycard);

There are two ways to display a component in a method provided by the CardLayout class:
(1) Use the code in the form of Show (container name, component designator) to specify the display of a component in a container. For example, the following code specifies the component designator K of the container p, which displays this component:
Mycard.show (P,K);
(2) Display the components in the order in which they are added to the container.
First (container): For example, Code Mycard.first (p);
Last (Container): For example, Mycard.last (p);
Next (Container): For example, Mycard.next (p);
Previous (Container): Mycard.previous (P);

The "Example 11-7" applet uses the CardLayout layout, and the Panel container p uses the CardLayout layout policy to set 10 label components. The window has 4 buttons that show the first component of P, the last component, the previous component of the current component, and the last component of the current component (view source file). five. Null layout and SetBounds method

An empty layout is one that sets the layout of a container to a null layout. The empty layout uses the SetBounds () method to set the size of the component itself and its position in the container:
SetBounds (int x,int y,int width,int height)
The component occupies a rectangle, and the parameter x, Y is the position coordinate of the upper-left corner of the component in the container, and the parameter weight,height is the width and height of the component. The method of placing components in an empty layout is two steps: Use the Add () method to add components first. Then call the SetBounds () method to set the position of the component in the container and the size of the component itself. Other methods related to components: GetSize (). Width, GetSize (). Height setvgap (ing vgap) sethgap (int hgap);

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.