Introduction
When users use Java Swing for user interface development, they encounter problems with how to lay out controls on Java swing. Swing controls are placed in a container (Container), which is a class that can hold a control or other container, with a specific example of a container Frame, a Panel, and so on. The container needs to define a layout manager to manage the controls, and the main layout managers offered in Swing are FlowLayout, BorderLayout, BoxLayout, GridLayout, and GridBagLayout. Their main features are shown in table 1:
This article focuses on the use of the BoxLayout layout manager. Let's first introduce the boxlayout.
BoxLayout Introduction
As mentioned earlier, BoxLayout can arrange the layout horizontally or vertically, which is determined by the parameters X_axis, Y_axis. The X_axis represents a horizontal arrangement, while the Y_axis represents a vertical arrangement. The BoxLayout constructor has two parameters, one parameter defines the container that uses the BoxLayout, and the other specifies whether the boxlayout is arranged horizontally or vertically. The following is an example of creating a BoxLayout instance:
JPanel panel=new JPanel ();
BoxLayout layout=new boxlayout (panel, Boxlayout.x_axis);
Panel.setlayout (layoout);
In this example, an instance of the BoxLayout Layout manager layout is created, and this instance is set to the panel's layout manager, which uses a horizontal arrangement to arrange the controls.
When BoxLayout is laid out, it places all controls horizontally or vertically, sequentially, in the order of the control's precedence, and if the entire horizontal or vertical space of the layout cannot drop all controls, then BoxLayout tries to resize the individual controls to fill the entire layout Horizontal or vertical space.
BoxLayout is often used in conjunction with Box, the reason for this is that BoxLayout is the control in a horizontal or vertical direction one after the placement, if you want to adjust the space between these controls, you will need to use the box container to provide transparent components as Fills to fill space between controls to adjust the spacing between controls. The Box container provides 4 transparent components, namely rigid area, strut, glue, filler. The Box container provides separate ways to create these components. The features of these four components are as follows:
Rigid area is a transparent component in which users can define horizontal and vertical dimensions;
The strut is similar to the rigid area, but the user can only define the dimensions of One direction, horizontal or vertical, and cannot define both horizontal and vertical dimensions;
When a user places a glue between two controls, it occupies as much space as possible between two controls, squeezing two controls to both sides;
Filler is the inner class of Box that, like the rigid area, can specify a horizontal or vertical dimension, but it can set the minimum, maximum, and priority dimensions.
Layout with BoxLayout
After understanding the basic features of boxlayout and box containers, let's take a look at the advantages of BoxLayout, first boxlayout can be vertical or horizontal layout of the control, while the boxlayout is simpler to use, however, combining it with the box container, it Can be a more complex layout, with the same effect as the use of gridbaglayout, but it is easy to use more easily. We use the button layout as an example to see how to use BoxLayout and Box containers for layout:
Figure 1. Apply BoxLayout for button Layout Example 1