Code SWT OF: skyhit dispatch time: 2004.05.17
SWT (Standard Widget Toolkit) is an open source GUI programming framework, with AWT / SWING, the famous open source IDE-Eclipse is developed with SWT, if you want to know the difference between SWT and AWT / SWING, please Check the related articles. SWT's entire source code You can download from www.eclipse.org, in order to read the source code, you can use the source code reading tool, Source Insight is a good choice. First, the main structure function of the code is divided. ORG.ECLIPSE.SWT has SWT, SWTEXCEPTION and SWTERROR classes, providing SWT constants and support for exception processing, and SWT has great use in constructor; 2, org.eclipse.swt.widgets package is common, the core SWT window widget (Widget) is defined. Such as Display, Shell, Button, Menu, etc. We generally write the GUI program to use these widgets; Wait, there is a default Adapter implementation class and EVENT classes with these corresponding Listener. 4, org.eclipse.swt.layout package is the class where the GUI program Layout is controlled (of course, related structural data classes), including FillLayout, GridLayout and RowLayout; 5, org.eclipse.swt.graphics package The GRAPHIC class is included in SWT, such as Color, Font, Font and Image, etc., org.eclipse.swt.printer provides support for printing services; 7, org.eclipse.swt.ole.win32 provides Win32 Ole in SWT Some classes of implementation include some customizable widgets in the org.eclipse.swt.custom package, which is very helpful for developing complex GUI programs; 9, org.eclipse.swt.dnd provides Drag and DROP support. Pay attention to the general GUI programming, org.eclipse.swt.widgets, org.eclipse.swt.events and org.eclipse.swt.Layout three packages are the most basic, most important . Second, SWT code research
Import org.eclipse.swt. *;
Import org.eclipse.swt.widgets. *;
Public class swthello {
Public static void main (String [] args) {
/ *
* Display example is used to manage the connection between SWT and the underlying operating system,
* The most important function is to implement SWT Event according to the event processing model of the platform.
* Loop, in general, as long as a Display is instance.
* Note that you need to create a Display instance before creating any WINDOW (shell instance),
* Remove the Display instance when the shell instance is closed
* /
Display display = new display () and DISPLAY ();
/ *
* Shell is the main window
* /
Shell shell = new shell (display);
/ *
* SWT.NONE is Sytle Bit for indicating the style of Widget's Style
* /
Label label = new label (shell, swt.none);
Label.Settext ("Hello");
Shell.pack ();
Label.pack ();
shell.open ();
While (! shell.isdisposed ())
{
IF (! display.readDispatch ())
Display.sleep ();
}
shell.dispose ();
}
}
About Resource Disposal 1, if you create a Widget or Graphic object with a constructor, you have to manually dispose to drop it; 2, if you don't use the constructor to get this widget or graphic object, because you are not you allocate You don't need to manually come to Dispose to drop it; Close off a shell, then this shell and its son widget will be recursive from Dispose, although you don't need to drop those widgets, but you have to drop image resources related to these widgets; 5, if you are in a Widget life The Graphic object is created in the period, which can be registered with a Dispose Listener to free this Graphic object, but the data objects such as Rectangle and Point do not use the operating system resources. Do not manually dispose (they do not have a Dispose method).