Just as we use layout in SWT to control the placement of various controls, it is best to hand over this work to layoutmanager in draw2d. Unless in the self-implemented layoutProgramDo not use the setbounds (), setlocation (), and setsize () methods to control the position and size of the image. Instead, after setting the appropriate layoutmanager for each image, use setconstraint () and setpreferredsize () to tell layoutmanager how to layout.
When necessary, the layout manager of the parent graph is responsible for modifying the position and size of each sub-graph. However, the calculation of the size of each sub-graph may be done by the layoutmanager of the sub-graph, the calculation method is generally reflected in the getpreferredsize () method of layoutmanager.
For example, when the parent graph uses xylayout and the Child graph uses toolbarlayout, assuming that the child graph (the Child graph in the Child graph) is added to the Child graph, add () the method will cause the revalidate () call. In this case, the xylayout of the parent image checks whether the child image has a constraint. If there is at least one direction-1, the toolbarlayout on the subgraph is used to calculate the new size of the Child image, this size is related to the number of child images contained in the child image (toolbarlayout adds the width/height of each child image, adds the space between them, and then considers the border of the image, returned size ).
Xylayout's implementation of the layout (ifigure) method:
Public Void Layout (ifigure parent ){
Iterator children = Parent. getchildren (). iterator ();
Point offset = Getorigin (parent );
Ifigure F;
While (CHILDREN. hasnext ()){
F = (Ifigure) children. Next ();
Rectangle Bounds = (Rectangle) getconstraint (f ); // Therefore, you must specify a constraint for the child image.
If (Bounds = Null ) Continue ;
If (Bounds. Width = - 1 | Bounds. Height = - 1 ){
Dimension preferredsize = F. getpreferredsize (bounds. Width, bounds. Height );
Bounds = Bounds. getcopy ();
If (Bounds. Width = - 1 )
Bounds. Width = Preferredsize. width;
If (Bounds. Height = - 1 )
Bounds. Height = Preferredsize. height;
}
Bounds = Bounds. gettranslated (offset );
F. setbounds (bounds );
}
}
The difference between setpreferredsize (dimension) and setsize (dimension) of the Figure Class in draw2d is that the setsize () method does not call the revalidate () method to re-layout, but only calls repaint () repaint the dirty area ). The setpreferredsize () method can be approximately equal to the setsize () method + revalidate () method, because in figure's implementation of getpreferredsize (INT, INT), if the figure does not have any layoutmanager, returns the current size:
Public Dimension getpreferredsize ( Int Whint, Int Hhint ){
If (Prefsize ! = Null )
Return Prefsize;
If (Getlayoutmanager () ! = Null ){
Dimension D = Getlayoutmanager (). getpreferredsize ( This , Whint, hhint );
If (D ! = Null )
Return D;
}
Return Getsize ();
}
Just take a look at toolbarlayout. java will know that toolbarlayout is not interested in constraint. The getconstraint () that calls toolbarlayout will always return NULL values. Therefore, we do not have to set constraint for the Child graphics that are placed in the graph using toolbarlayout. Therefore, if we have a picture, a contains B and B contains C, how can we automatically change the size of B (using toolbarlayout) with the number of C? This depends on the layoutmanager used by A. If toolbarlayout is used, no special settings are required. If xylayout is used, a is used. getlayoutmanager (). the setconstraint (B, new rectangle (X, Y,-1,-1) Statement sets constraint for A, and uses setpreferredsize () to specify the actual size for image C.
Click here to download a layout example, as shown below.