-As you know, containers (such as JFrame, JPanel, ...) are used to hold controls in them, but they do not have or default to arrange objects that are not what we want. So we need to use layouts to do this. In other words, Layouts allow us to arrange the controls in the containers in a logical and beautiful way.
-Java swing boxlayout is a layout that allows you to arrange controls on a horizontal row or on a vertical column.
-When we align in a horizontal line, the controls will not automatically go down if there is not enough room for them.
-Java swing boxlayout is a layout that allows you to arrange controls on a horizontal row or on a vertical column.
-When we align in a horizontal line, the controls will not automatically go down if there is not enough room for them.
Example of creating and using java swing boxLayout
-We will have a sequence of JButtons, which has a JButton of Change that will allow us to convert the display from horizontal to vertical and vice versa.
The output:
-You notice in the previous post may be we just initialized JPanel and set the Layout to it immediately in the form: [JPanel panel = new JPanel (new LayoutManager ());
-But with java swing boxLayout we can not do that because BoxLayout's initialization function is our container.BoxLayout (Container target, int axis):
-BoxLayout(Container target, int axis): Creat BoxLayout with a arrangement is axis.
-So we need to initialize the container (JPanel) first, then initialize java swing boxLayout and finally use setLayout to set the layout for JPanel.The placement of horizontal or vertical objects depends on the axis argument, X_AXIS will be arranged horizontally, Y_AXIS is vertical.
Comments
Post a Comment