The previous article explains JFrame, JPanel, which already involves the use of empty layouts. While Java can pinpoint components in pixels, there will be some display differences in different systems, so that the display is not the same, so Java provides a layout manager to make the graphical interface written with good platform independence.
Note: All layout managers are used for containers, including top-level containers and intermediate containers.
One, the layout manager belongs to the class package
Owning class Package
Layout Manager Name
Description
java.awt
FlowLayout (Flow layout)
The components are aligned from left to right according to the order in which they are joined, and a row is filled to the next line to begin the arrangement
BorderLayout (Border layout)
Containers are divided into five regions, east, west, south, north, and Central, where only one component can be placed.
GridLayout (Grid layout)
The container's space is divided into a grid area of the MXN column, where only one component can be placed per region.
CardLayout (Card layout)
Like a stack of cards, each card corresponds to one component, but only one card at a time can be displayed. For situations where multiple components are prevented in one space
GridBagLayout (Grid package layout)
GridLayout, components are still placed in rows and columns, but each component can occupy multiple meshes
Java.swing
BoxLayout (Box-type layout)
Allows multiple controls to be prevented vertically or horizontally in the container
Spriglayout (Spring Layout)
Placing controls based on a set of constraints
No
Empty layout
Place controls by the size, location information provided by the control itself, without using the layout manager
Second, the container's default layout manager
Each container has a default layout management, as shown in the following table:
Container
Default layout mode
Top-level containers
JFrame
BorderLayout (Border layout)
JDialog
BorderLayout (Border layout)
JApplet
FlowLayout (Flow layout)
Intermediate container
JPanel
FlowLayout (Flow layout)
Three, FlowLayout (flow-type layout)
The components in a container using the FlowLayout layout are arranged in the order in which they are joined (centered, left, right, aligned) from left to right, with a row full (that is, after the component exceeds the container width) to the next line to begin the arrangement.
1, the flow layout features are as follows:
L components are arranged according to the alignment of the set
L Regardless of alignment, the components are arranged from left to right, one row full, and the next line is transferred. (for example, in the right-aligned arrangement, the first component at the far right of the first line, the second component is added, the first component moves to pan left, the second component becomes the rightmost component of the line, which is the left-to-right arrangement)
2. Flow layout common constructors and methods of FlowLayout class
constructor function
Name
Use
FlowLayout ()
Constructs a new flowlayout, which is the default center-aligned, with the default horizontal and vertical clearance being 5 pixels
FlowLayout (int align)
Constructs a new flowlayout that has the specified alignment, and the default horizontal and vertical clearance is 5 pixels
The five parameter values and meanings are as follows:
0 or Flowlayout.left, control left justified
1 or Flowlayout.center, center-aligned
2 or Flowlayout.right, right-justified
3 or flowlayout.leading, the control corresponds to the start edge of the container direction
4 or flowlayout.trailing, the control corresponds to the end edge of the container direction
Left-justified if it is an integer other than 0, 1, 2, 3, 4
FlowLayout (int align, int hgap, int vgap)
Creates a new flow layout manager that has the specified alignment as well as the specified horizontal and vertical clearances.
Method
Name
Use
Void setalignment (int align)
Sets the alignment for this layout.
void Sethgap (int hgap)
Sets the horizontal gap between components and the edges of the component and the Container.
void Setvgap (int vgap)
Sets the vertical gap between components and the edges of the component and the Container.
3. FlowLayout Layout Application Code Snippet Example
1) Set FlowLayout layout
JFrame fr=new JFrame ();
FlowLayout flow=new FlowLayout ();
Fr.setlayout (flow);
The above statement can be simplified into:
Fr.setlayout (New FlowLayout ());
2) Set the FlowLayout layout of the frame fr to the left alignment of the component
Fr.setlayout (Newflowlayout (flowlayout.left));
3) Set the frame fr to the left-aligned flowlayout layout of the component, and the component's horizontal spacing is 20 pixels and the vertical spacing is 40 pixels.
Fr.setlayout (New FlowLayout (flowlayout.left,20,40));
Example: Alignment
Flowlayoutdemo.java
importjavax.swing.*;
importjava.awt.*;
Public Classflowlayoutdemo extends JFrame {
Public Flowlayoutdemo () {
Set the form as a streaming layout with no parameters default to center-aligned
SetLayout (New FlowLayout ());
Set the font style that is displayed in the form
SetFont (New Font ("Helvetica", Font.plain, 14));
Add a button to a form
Getcontentpane (). Add (Newjbutton ("button 1"));
Getcontentpane (). Add (New JButton ("button 2"));
Getcontentpane (). Add (New JButton ("Button3"));
Getcontentpane (). Add (Newjbutton ("button 4"));
}
public static void Main (String args[]) {
Flowlayoutdemo window = Newflowlayoutdemo ();
Window.settitle ("Flow-style layout");
The code sets the size of the window to fit all the components you place
Window.pack ();
Window.setvisible (TRUE);
Window.setdefaultcloseoperation (Jframe.exit_on_close);
Window.setlocationrelativeto (NULL); To center the form on the display
}
}
As shown in the program execution results, a center-displayed form is generated with four buttons on it, and the buttons and the left and right edges of the form are the default 5-pixel spacing. Change the size of the form as shown in the original interface, widen the original interface, narrow the original interface, and the spacing between the components and the interface.
Modify the program code experience the interface layout effect:
SetLayout (Newflowlayout ());
Change the code in the above source program as follows, and then make the following changes:
SetLayout (newflowlayout (0)); Component left Justified
SetLayout (Newflowlayout (flowlayout.right,10,15)); Component right-aligned, horizontal spacing between components is 10 pixels, vertical spacing is 15 pixels
FlowLayout Java Graphical interface design-layout manager (Flow layout)