Packagecom.swing;ImportJava.awt.Container;Importjava.awt.FlowLayout;ImportJavax.swing.JButton;ImportJavax.swing.JFrame;Importjavax.swing.WindowConstants;/*** 1: The flow (flowlayout) Layout Manager is the most basic layout manager in the layout manager, and the flow layout manager's layout in the entire container is the same as its name, and the component is placed from left to right, like a stream, until it occupies all the space in the line, and then moves down one line. The HORIZGAP parameter specifies the horizontal split between components in pixels * The VERTGAP parameter specifies the vertical split between components in pixels * *@authorBiexiansheng **/ Public classFlowlayoutpositionextendsjframe{ Publicflowlayoutposition () {Settitle ("This form uses the Flow Layout manager");//set the form titleContainer Container=getcontentpane ();//Initialize Container//The setup form uses the Flow Layout manager to align components to the right, and to set horizontal intervals between components, and vertical intervalsSetLayout (NewFlowLayout (1,10,10));//new A construction method//FlowLayout The first parameter is set to 0 each row of components is arranged in the left alignment//FlowLayout The first parameter is set to 1 each row of components is aligned in the middle//FlowLayout The first parameter is set to 2 each row of components is arranged in the right alignment for(inti=0;i<10;i++) {//add 10 buttons to a containerContainer.add (NewJButton ("button" +i)); //New A button construction method, the use of skills and practice} setSize (300,200);//set the size of a formSetVisible (true);//set up form visualization//set how forms are closedsetdefaultcloseoperation (windowconstants.exit_on_close); } Public Static voidMain (string[] args) {flowlayoutposition fl=Newflowlayoutposition (); //Initialize Object Call construction method }}
The results of the instance run are
Packagecom.swing;Importjava.awt.BorderLayout;ImportJava.awt.Container;ImportJavax.swing.JButton;ImportJavax.swing.JFrame;Importjavax.swing.WindowConstants;/*** 1: In the case of default form layout is not specified, the Swing component layout mode is boundary (borderlayout) * Layout Manager * 2: The Boundary layout manager can also divide the container into 5 regions in east, you can add components to the 5 zones * The Boundary layout manager places the label in the middle of the form, and the entire component occupies all the space in the form * * 3: The container calls the Add () method of the container class to add components when the component can be set in the Boundary Layout manager area * Control of a zone can be determined by a member variable in the BorderLayout class *@authorBiexiansheng **/ Public classBorderlayoutpositionextendsjframe{//an array that defines where the components are placedString[] Border={borderlayout.center,borderlayout.north, borderlayout.south,borderlayout.west,borderlayout.east}; /*1: Place the layout and the component name in the array, and then set the container to use the Boundary layout Manager * Finally add the button to the container in the loop and set the component layout the *2:add () method provides the ability to add components to the container and also to set the placement position of the component * */ //defines the text above the button component Eswn North-West right EastString[] buttonname={"center button", "North button", "South Button", "West Button", "East Eastern button",}; PublicBorderlayoutposition () {//defining a construction methodSettitle ("This form uses the Boundary layout manager");//Container Container=getcontentpane ();//Define a containerSetLayout (NewBorderLayout ());//set container as boundary layout manager//setlayout () syntax for setting up layout manager//Add a button to the container and set the button layout for(inti=0;i<border.length;i++) {Container.add (border[i],NewJButton (Buttonname[i])); } //set external properties of a formSetSize (350,200);//set the size of a formSetVisible (true);//Set Form Visible//set how forms are closedsetdefaultcloseoperation (windowconstants.exit_on_close); } Public Static voidMain (string[] args) {//TODO auto-generated Method StubBorderlayoutposition bl=Newborderlayoutposition (); //The constructor method is called when instantiating an object }}
The results of the instance run are
Packagecom.swing;ImportJava.awt.Container;Importjava.awt.GridLayout;ImportJavax.swing.JButton;ImportJavax.swing.JFrame;Importjavax.swing.WindowConstants;/*** 1: Grid (GridLayout) Layout manager divides containers into meshes, so components can be arranged by rows and columns * In the grid Layout manager, each component is the same size, and the number of grid cells is determined by the number of rows and columns in the grid * A grid of two rows and two columns can produce 4 meshes of equal size, * * 2: The component starts from the upper left corner of the grid, is added to the grid in order from left to right, top to bottom, and each component fills the entire grid, changes the size of the form, and the size of the component changes. * 3: The Grid Layout Manager has 2 main construction methods * The rows parameter represents the number of lines in the grid * The columns parameter represents the column count of the grid, and the 2 parameters have only one row or one column can arrange any number of components * Parameters Horizgap Specify the spacing between the grids. Specify horizontal spacing between grids * parameter vertgap Specify the vertical spacing between grids *@authorBiexiansheng **/ Public classGridlayoutpositionextendsjframe{ PublicGridlayoutposition () {//defining a construction methodContainer Container=getcontentpane ();//Define a container//set up a grid using the Grid layout manager with 7 rows and 3 columnsSetLayout (NewGridLayout (7,3,5,5));//start setting up layout manager First for(inti=0;i<20;i++) {Container.add (NewJButton ("button" +i)); } //set the external structure of the containerSettitle ("This is a form for a grid layout manager"); SetSize (300,300);//set the size of a formSetVisible (true);//to set the visible form//set how forms are closedsetdefaultcloseoperation (windowconstants.exit_on_close); } Public Static voidMain (string[] args) {//TODO auto-generated Method StubGridlayoutposition gl=Newgridlayoutposition (); //Call construction method when initializing an object }}
The results of the instance run are
Java Learning Swing Chapter flow layout manager Simple Learning