JAVA GridBagLayout layout manager application details, javatedbaglayout
In many cases, you do not need to write code to implement a graphical interface of an application. Instead, you can easily use a powerful IDE tool to drag and drop the Code with simple event processing code. However, we have to deal with some problems in this operation. Sometimes we want to freely change the size of a program interface. The interface generated by the drag-and-drop interface often does not provide this function, once a custom interface changes its shape, the layout between components will become messy.
The layout manager in Java applies Strategy to provide a good model for different types of component la S. The Grid group layout manager (GridBagLayout) is considered to be the most powerful among all layout managers. The following uses an example to describe how to use it.
1. Implement a frame of drawing board under WindowsXP. below is the design model diagram.
We can regard GridBagLayout as a grid layout without any constraints or restrictions. A component can occupy several rows and columns, and the size setting is free. We can clearly see that the entire graphic board interface is divided into four rows and two columns of a table. We don't have to worry about the size of a cell, just divide it.
In this way, the five specific panels (we use the JPanel object to fill in) constitute the entire interface, with the top side occupying one row and two columns of the Tool Selection Panel (toolSelectPanel ), toolConcretePanel, drawPanel on the right, colorPanel and statePanel on the bottom
2. perform the following steps for GridBagLayout layout:
1) set the layout manager of the main interface to GridBagLayout (No rows or columns need to be specified)
2) specify a GridBagConstraints object for each component in the interface (JPanel object here), and indicate that the component is in
Layout Scheme in Manager
3). Add the extremely restrictive condition (GridBagConstraints object) of the component through the following call)
Add (Component, constraints );
It is necessary to understand the specific meaning of each attribute in GridBagConstraints so that we can better customize the layout.
@ Gridx, gridy:
Position of the component in the upper left corner. For example, if the panel on the left is in the column 0 in the row 1, gridy = 0 and gridx = 1. The row corresponds to gridy, and the column corresponds to gridx.
@ Gridwidth, gridheight
The number of rows and columns occupied by the component. For example, if the top panel occupies one row and two columns, gridwidth = 2, gridheight = 1
@ Weightx, weighty
It can be simply understood as the increment value of the component size change. For example, if weightx = 100 is set, the component size changes with the cell. When weightx is set to 0, the component size does not change. Of course, weightx and weighty can also be set to other values, but it is of little significance and will not be described in detail.
@ Fill
Filling mode of components in the grid (Allocation Area)
For example, if fill = HORIZONTAL is used, the component fills up the full cells horizontally. If fill = BOTH is used, the entire grid is filled.
@ Anchor
When a component is located in the grid, anchor = EAST indicates the right alignment.
@ Ipadx, ipady
Internal filling refers to adding ipadx to the x direction and ipady to the y direction based on the preferred size of the component. This ensures that the component will not contract to ipadx, the size determined by ipady is below the size, so we can use the value of ipadx and ipady to specify the size of the component, instead of the size of the component, otherwise it will have an unexpected effect.
@ Insets
The filling area is the part between the component and the grid border. Four parameters are left, top, right, and bottom. However, when the fill of the component is set to NONE, the specified insects value is meaningless.
3. The code is implemented below. Let's take a look at it first.
This is the interface generated by running.
This is the interface after stretching
The key code is as follows:
Java code
- Private void addGridBagPanes (){
- // Select the upper-side tool panel
- JPanel toolSelectPanel = new JPanel ();
- ToolSelectPanel. setBackground (Color. green );
- This. add (toolSelectPanel, new GBC (0, 0, 2, 1 ).
- SetFill (GBC. BOTH). setIpad (200, 50). setWeight (100, 0 ));
- // Specific tool panel on the left
- JPanel toolConcretePanel = new JPanel ();
- ToolConcretePanel. setBackground (Color. YELLOW );
- This. add (toolConcretePanel, new GBC (0, 1 ).
- SetFill (GBC. BOTH). setIpad (70, 90). setWeight (0,100 ));
- // Drawing panel on the right
- JPanel drawPanel = new JPanel ();
- DrawPanel. setBackground (Color. WHITE );
- This. add (drawPanel, new GBC (1,1). setFill (GBC. BOTH ));
- // Lower-side Color Selection Panel
- JPanel colorPanel = new JPanel ();
- ColorPanel. setBackground (Color. LIGHT_GRAY );
- This. add (colorPanel, new GBC (0, 2, 2, 1 ).
- SetFill (GBC. BOTH). setIpad (100, 50). setWeight (, 0 ));
- // Status panel on the lower side
- JPanel statePanel = new JPanel ();
- StatePanel. setBackground (Color. CYAN );
- This. add (statePanel, new GBC (0, 3, 2, 1 ).
- SetFill (GBC. BOTH). setIpad (200, 20). setWeight (100, 0 ));
- }
The GBC class inherits from GridBagConstraints. This aims to simplify the complexity of each direct operation on the GridBagConstraints object, each set method in GBC returns a GBC object, so you can call the set Method in succession. The GBC code is as follows:
Java code
- Public class GBC extends GridBagConstraints
- {
- // Initialize the upper left corner
- Public GBC (int gridx, int gridy)
- {
- This. gridx = gridx;
- This. gridy = gridy;
- }
- // Initialize the position and number of rows and columns in the upper left corner.
- Public GBC (int gridx, int gridy, int gridwidth, int gridheight)
- {
- This. gridx = gridx;
- This. gridy = gridy;
- This. gridwidth = gridwidth;
- This. gridheight = gridheight;
- }
- // Alignment
- Public GBC setAnchor (int anchor)
- {
- This. anchor = anchor;
- Return this;
- }
- // Whether to stretch and stretch
- Public GBC setFill (int fill)
- {
- This. fill = fill;
- Return this;
- }
- // Increment in the x and y directions
- Public GBC setWeight (double weightx, double weighty)
- {
- This. weightx = weightx;
- This. weighty = weighty;
- Return this;
- }
- // External filling
- Public GBC setInsets (int distance)
- {
- This. insets = new Insets (distance, distance );
- Return this;
- }
- // Fill out
- Public GBC setInsets (int top, int left, int bottom, int right)
- {
- This. insets = new Insets (top, left, bottom, right );
- Return this;
- }
- // Inner Filling
- Public GBC setIpad (int ipadx, int ipady)
- {
- This. ipadx = ipadx;
- This. ipady = ipady;
- Return this;
- }
- }