JAVA learning Swing chapter flow layout manager simple learning, swing chapter
Package com. swing; import java. awt. container; import java. awt. flowLayout; import javax. swing. JButton; import javax. swing. JFrame; import javax. swing. windowConstants;/*** 1: The FlowLayout layout manager is the most basic layout manager in the layout manager. The layout of the flow layout manager in the entire container is just like its name, place components from left to right like a stream until it occupies all the space of this row, and then move one row down. * By default, components are arranged in the center of each row, but you can also change the position of the component on each row through settings. ** 2: in the FlowLayout class, there are three constructor methods. * The alignment parameter uses the stream layout manager to specify the position of the component in each row. * The horizGap parameter specifies the horizontal segmentation between components in pixels. * The vertGap parameter uses vertical Split of pixels between specified components ** @ author biexiansheng **/public class FlowLayoutPosition extends JFrame {public FlowLayoutPosition () {setTitle ("this form uses the stream layout manager"); // set the Form title Container container = getContentPane (); // initialize the Container // set the form to use the stream layout manager, align the right of the component and set the horizontal interval between the component and the vertical interval setLayout (new FlowLayout (, 10 )); // new constructor // The first parameter of FlowLayout is set to 0. Each line of component is arranged in the left alignment. // The first parameter of FlowLayout is set to 1. Each line of component is arranged in the middle alignment. // The first parameter of FlowLayout is set to 2. Each line of components is arranged in the right alignment for (int I = 0; I <10; I ++) {// Add 10 buttons to the container. add (new JButton ("button" + I); // a new button constructor. This kind of skill is used to work hard} setSize (300,200 ); // set the size of the form setVisible (true); // set the form visualization // set the form closing method setdefaclocloseoperation (WindowConstants. EXIT_ON_CLOSE);} public static void main (String [] args) {FlowLayoutPosition fl = new FlowLayoutPosition (); // initialize the object call construction method }}
The instance running result is as follows:
Package com. swing; import java. awt. borderLayout; import java. awt. container; import javax. swing. JButton; import javax. swing. JFrame; import javax. swing. windowConstants;/*** 1: by default, the layout mode of the Swing component is BorderLayout. * layout manager ** 2: the border layout manager can also divide the container into five areas in the east, west, and south, and add components to these five areas * The border layout manager can place the labels in the middle of the form, and the entire component occupies all the space of the form ** 3: The Container calls the add () of the Container class () when adding a component, you can set the control of the region * of this component in the border layout manager. It can be determined by the member variables in the BorderLayout class * @ author biexiansheng **/public class BorderLayoutPosition extends JFrame {// define the array String [] border = {BorderLayout. CENTER, BorderLayout. NORTH, BorderLayout. SOUTH, BorderLayout. WEST, BorderLayout. EAST};/* 1: place the layout and component names in the array respectively, set the container to use the border layout manager *, and add the button to the container in the loop, and set the component layout. * 2: add () provides the function of adding components to the container, at the same time, set the placement position of the component ** // define the text ESWN above the button component. The text is in the southeast, northwest, upper, North, lower, left, West, right, east, String [] buttonName = {"center button ", "north button", "south button", "west button", "east button",}; public BorderLayoutPosition () {// define a constructor setTitle ("this form uses the border layout manager"); // Container container = getContentPane (); // define a container setLayout (new BorderLayout (); // set the container as the border layout manager // setLayout () syntax to set the layout manager // Add a button in the container, and set the button layout for (int I = 0; I <border. length; I ++) {container. add (border [I], new JButton (buttonName [I]);} // set the external attribute setSize (350,200) of the form ); // set the size of the form setVisible (true); // set the form to be visible // set the form closing method setdefaclocloseoperation (WindowConstants. EXIT_ON_CLOSE);} public static void main (String [] args) {// TODO Auto-generated method stub BorderLayoutPosition bl = new BorderLayoutPosition (); // The constructor is called when the object is instantiated }}
The instance running result is as follows:
Package com. swing; import java. awt. container; import java. awt. gridLayout; import javax. swing. JButton; import javax. swing. JFrame; import javax. swing. windowConstants;/*** 1: The grid layout manager divides containers into grids, so the components can be arranged by row and column * in the grid layout manager, each component has the same size, and the number of spaces in the grid is determined by the number of rows and columns in the grid. * For example, a grid with two rows and two columns can generate four equal-size grids, ** 2: The component is added to the grid from the upper left corner of the grid from left to right, from top to bottom, and each component is filled with * the entire grid, changing the size of the form, the size of the component also changes ** 3: the grid layout manager mainly has two constructor Methods * The rows parameter represents the number of rows in the grid * The columns parameter represents the number of columns in the grid, only one of these two parameters can be 0, indicating that one row or one column can arrange any number of components. * The horizGap parameter specifies the spacing between grids, horizontal grid spacing * parameter vertGap vertical grid spacing * @ author biexiansheng */public class GridLayoutPosition extends JFrame {public GridLayoutPosition () {// define a constructor Container = getContentPane (); // define a container // set the form to use the grid layout manager, setLayout (new GridLayout (,) with seven rows and three columns set; // set the layout manager for (int I = 0; I <20; I ++) {container. add (new JButton ("button" + I);} // set the external structure of the container setTitle ("this is the form of a grid layout manager"); setSize (300,300 ); // set the size of the form setVisible (true); // set the visible form // set the form closing method setdefaclocloseoperation (WindowConstants. EXIT_ON_CLOSE);} public static void main (String [] args) {// TODO Auto-generated method stub GridLayoutPosition gl = new GridLayoutPosition (); // call the constructor when initializing the object }}
The instance running result is as follows: