Notes for SWT Layout
In the past two days, I led the team members to develop a very complex interface. Because the team members are not very familiar with SWT layout, many problems have emerged on the interface. Sometimes an exception occurs, because layout and layoutdata do not match, and sometimes the screen is not displayed. Always, the problems that can be generated are basically met.
The current development idea is to first draw the interface with SWT designer and then manually rebuild and organize it. SWT designer cannot identify the restructured code. Therefore, it is not feasible to rely solely on visualization tools, but also to be very familiar with SWT interface encoding. Therefore, it is critical to master SWT layout.
Note the following when creating your own composite:
1. The composite passed in from the outside must be used only once, that is, when super (parent, style); is used, and the father of all the controls in the future will be the composite itself. Do not use parent anywhere else. Otherwise, it is likely to interfere with the content in the parent, resulting in display Errors for itself and other controls in the parent.
2. layout must be set for each composite container; otherwise, nothing may be displayed. In general, gridlayout is set. If Composite itself places a control, for example, placing a table, filllayout can also be used. Gridlayout is recommended for beginners. Otherwise, layout and layoutdata do not match easily. If the interface is complex, the problem is hard to find. Gridlayout can completely replace other layout to meet various requirements (if there are overlapping controls, it will not work, and formlayout should be used ).
The following is a piece of source code. We recommend that you follow this method to build the interface.
Public class testcomposite extends composite ......{
Private text;
/***//***//***//**
* Create the Composite
* @ Param parent
* @ Param Style
*/
Public testcomposite (composite parent, int style )......{
Super (parent, style );
Setlayout (New gridlayout ());
Createarea (this );
}
Private void createarea (composite parent )......{
TEXT = new text (this, SWT. Border );
Text. setlayoutdata (New griddata (SWT. Fill, SWT. Center, true, false ));
Final button = new button (this, SWT. None );
Button. settext ("button ");
}
@ Override
Public void dispose ()......{
Super. Dispose ();
}
}