In the interface design, a container must be placed in many components. For the sake of beauty, it is arranged in the container for components. This is the layout design. Java.awt defines a variety of layout classes, each layout class corresponds to a layout strategy. The following layout category:
• FlowLayout, placed components in turn.
• Boarderlayout, put the components on the boundary.
• Cardlayout, stack components like poker cards, and can only display one of the components each time.
• GridLayout divides the display area into equal grids according to lines and columns, and components are placed in these grids in turn.
• GridbagLayout divides the display area into many small units, and each component can occupy one or more small units.
Among them, GridbagLayout can perform fine position control, and the most complicated. This tutorial will not discuss this layout strategy for the time being, and will explain it in detail in a special article.
Each container has a layout manager, and it decides how to arrange components in the container. The layout manager is a class that implements the LayoutManager interface.
1.FlowLayout layout
The FlowLayout layout is to arrange the components from left to right according to the order of the addition, and then transfer to the line to continue from left to right after one line, and the components in each line are arranged in the middle. This is the easiest layout strategy. It is generally used in the case where there are not many components. When there are many components, the components in the container will appear uneven, and the governors are short.
FlowLayout is the default layout of small applications and panels.
1.FlowLayout (), generate a default FlowLayout layout. By default, the component is in the middle, with 5 pixels.
2. FlowLayout (int Aligment), set the alignment method of each component. Alignment can be available for FlowLayout.left, FlowLayout.Center, FlowLayout.richt.
3.FlowLayout (int Aligment, int Horz, int Vert), set the alignment method, and set the horizontal spacing Horz and vertical spacing vert of the component, set the layout of the container with the method of the super -class container. For example, the code setLayout (New FlowLayout ()) sets the FlowLayout layout for the container. The method of adding a component to the container is ADD (component name).
2. BorderLayout layout (Jwindow, JFRAME, JDialog's default layout)
The BorderLayout layout strategy is to simply divide the space in the container into the five areas of East "East", West "West", South "SOUTH", North "North", and "Center". When joining the component, you should indicate which area placed in which the component is placed. Put a component in a position. If you want to add multiple components at a certain location, you should first add the component of the location to place another container, and then add the container to this position.
The structure method of the borderlayout layout is:
(1) BorderLayout (), generate a default BorderLayout layout. By default, there is no gap.
(2) BorderLayout (int Horz, int Vert), set the horizontal and vertical spacing between the components.
The setting method of the BorderLayout layout strategy is setlayout (new borderlayout ()). The method of adding the component to the container is ADD (component name, location). If there is no specified position when adding the component, the default is the "middle" position.
The borderlayout layout is the default layout of Jwindow, JFRAME, JDialog.
[Example 11-5] The application has five labels, which are placed in the five areas of the east, west, south, north, and middle and middle, respectively.
Import javax.swing.*; Import Java.awt.*; Public Class J505 {Public Static Void Main (String [] ARGS) {jlabel1, label2, label4, label4, label5; Jframe mw = new jframe ("I am a one Window "); // Create a window container object mw.setsize (250,200); container con = mw.getContentPane (); con.setLayout (new borderLayout ()); Label1 = New Jlabel (" East Tag ");/ / / The default left to Qi Qi Label2 = New Jlabel ("South Tag", JLABEL.Center); Label3 = New Jlabel ("West Tag"); Label4 = New Jlabel ("North Tag", JLABEL.CENTER); Label5 = New Jlabel ("" " Mid -label ", jlabel.center); con.add (label1," set "); con.add (label2," south "); con.add (label3," west "); con.add (label4," North "); con.add (label5," center "); mw.setvisible (true);}}
3.GridLayout layout
The GridLayout layout is to divide the container into a grid -like grid shape. The number of rows and columns is controlled by the program. The characteristics of GridLayout layout are that the component positioning is more accurate. Since each grid in the GridLayout layout has the same shape and size, the components that are required to put in the container should also maintain the same size.
The constructive method of GridLayout layout is:
(1) GridLayout (), generate a single -column GridLayout layout. By default, there is no gap.
(2) GridLayout (int Row, int color), set a GridLayout layout with ROW and column Col.
(3) GridLayout (int Row, int color, int horz, int vert), set the number of rows and columns of the layout, the horizontal and vertical spacing of the component.
GridLayout's layout is based on behavioral benchmarks. When the number of components is excessive, the list is automatically increased. On the contrary, too few components will automatically reduce the column, the number of rows remains unchanged, and the components are arranged in the order of priority (automatically increased according to the component). Each grid of the GridLayout layout must be filled in the component. If you want a grid to be blank, you can use a blank label (new label ())).
[Example 11-6] Small applications first put several buttons and several labels in JPANEL, then put JPANEL in JScrollpane. Finally, put JSCROLLPANE in a small program window. With horizontal and vertical scroll bars, the visual range of the scroll panel is less than the actual requirements of the panel. The slider display panel that can move the scroll bar is not in the area within the visual range.
Import java.applet.*; Import Javax.swing.*; Import Java.awt.*; Class Mywindow Extends Jframe {Public Mywindow (Int W, Int H) {Settital Example "); Container Con = GetContentPane ( ); Seetpreferredsize (New Dimension (W, H)); con.setLayout (new borderlayout ()); jpanel P = New Jpanel (); p.Setlayout (new gridLayout (6,6)); NT i = 0; I <6; I ++) {p.add (new jlabel ()); for (int j = 1; j <= 2; j ++) {p.add (new jbutton ("button"+(2*i +j))); p.add (new jlabel ("" label "+(2*i+j));} p.add (new jlabel ());} p.SetBackground (color.blue); p. Setpreferredsize (New Dimension (W+60, H+60)); jscrollpane scrollpane = new jscrollpane (p); scrollpane.setpreferredsize ); add (scrollpane, borderlayout.center); // The applet is added to the rolling panel setvisible (true); Pack ();}} Class Scrolpane ExcrollPane {PUBLIC Scrollpane (component p) {Super (P); SetHorizontalsCro llbarPolicy (jscrolpane.horizontal_scrollbar_alway); SetverticalScrollBarPolicy (JSCROLLLLLTICAL_SCROLLLL_ALWAYS);} } Public Class J506 EXTENDS Applet {Mywindow Mywindow; Public Void Init () {Mywindow = New Mywindow (400, 350);}}
GridLayout layout requires the size of all components to be consistent, which may not be beautiful enough to use the interface appearance. A remedy is to combine some components in a container, then use this container as a component, and then put it in the GridLayout layout. This is the container nesting mentioned earlier. For example, container A uses GridLayout layout to divide the container into grids; other container B and C are put into several components, and B and C are added as components to container A, respectively. Container B and C can also be set to GridLayout layout, divide yourself into several grids, or can also be set to other layouts. In this way, from the appearance, the size of each component is different.
4.CardLayout layout
Although the container using the CardLayout layout can accommodate multiple components, multiple components have the same display space, and only one component can be displayed at a certain moment. Just like a stack of poker cards can only display the top one at a time, this displayed component will occupy all the space of the container. Cardlayout layout design steps are as follows:
First create a CardLayout layout object. Then, use the setLayout () method to set the layout for the container. Most, the ADD () method of calling the container adds the component to the container. The method of adding the component of the CardLayout layout strategy is:
ADD (component code, component);
The component code is a string, which is given separately and has nothing to do with the component name.
For example, the following code sets the CardLayout layout for a JPANEL container:
Cardlayout mycard = new cardLayout (); // Create the CardLayout layout object jpanel P = New Jpanel (); // Create a Panel object p.Setlayout (MyCard);
There are two ways to display a component in the method provided by the CardLayout class:
(1) Code in the form of show (container name, component code), and specify a component display in a container. For example, the following code specifies the component of the container P to display this component:
mycard.show (p, k);
(2) The component is displayed in order of adding the container according to the component.
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);
[Example 11-7] Small applications use CardLayout layout. Panel container P uses CardLayout layout strategy to set 10 tag components. The window has 4 buttons, which are responsible for displaying the first component of P, the last component, the first component of the current component, and the last component of the current component.
Import java.applet.*; Import java.awt.*; Import java.event.event.*; Import javax.swing.*; class mypanel extends jpanel {int x; my Panel (int a) {x = A ; Getsize (); Label1 = New JLABEL ("I am"+x+"a label"); add (label1);} Public Dimension getpreferredsize () {RETURN New Dimension (200,50); IC Class J507 EXTENDS Applet Implements ActionListener {Cardlayout MyCard; MyPanel Mypanel []; JPANEL P; Private void Addbutton (JPANEL PAN, String ButName, ActionListener Listener) {JBUTTTTTTTT On Abutton = New Jbutton (ButName); Abutton.AdDactionListener (Listener); Pan.add (Abutton );} Public Void Init () {setLayout (new borderlayout ()); // The layout of the applet is the border layout mycard = new cardLayout (); this.Setsize (400,150); p = new jpanel (). ; P.Setlayout (MyCard); // P's layout is set to a card -type layout mypanel = new mypanel [10]; for (int i = 0; i <10; i ++) {mypanel [i] = new mypanel (i+1); P .add ("a"+i, mypanel [i]);} jpanel p2 = new jpanel (); addButton (p2, "first", this); addButton (p2, "last", this); addbutton (p2, "first one", this); addButton (p2, "latter one", this); add (p, "center"); add (p2, "south");} Public void actionPerformed (actionEvent E) { iftActionCommand (). Equals ("first") mycard.first (p); else if (e.getActionCommand (). Equals ("Last")); Else if); else if); else if); else if); (e.getActionCommand().equals("前一个"))mycard.previous(p); else if(e.getActionCommand().equals("后一个"))mycard.next(p); }}
5.NULL layout and Setbounds method
The air layout is to set the layout of a container to the NULL layout. The air layout uses the Setbounds () method to set the size of the component itself and the position in the container:
Setbounds (int x, int y, int width, int height)
The area occupied by the component is a rectangular, parameter x, and y is the position coordinate of the upper left corner of the component in the container; parameter weight, height is the width and height of the component. There are two steps for air layout resettlement components: first use the ADD () method to add components in the body container. 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:
1.getsize (). Width,
2.getsize (). Height
3.SetVGAP (ING VGAP)
4.Sethgap (int Hgap);